Skip to content

Conversation

@ishaquehassan
Copy link

@ishaquehassan ishaquehassan commented Mar 19, 2025

PR: Fix iOS Safari File Picker Issue with a Transparent Overlay

Summary

This PR resolves an iOS Safari limitation where a programmatic click (.click()) on an <input type="file"> is blocked. Instead of relying on a virtual click, this solution uses a custom dialog with a transparent, overlaid <input type="file"> element on the “Allow” button to ensure iOS recognizes it as a genuine user interaction.


Background

  • Problem: On iOS Safari, .click() triggers for file inputs are prevented for security reasons.
  • Impact: Users could not upload files on iOS devices if the file picker was triggered without direct user interaction.

Approach

  1. Custom Dialog: Displays a modal asking the user to "Allow" file selection.
  2. Transparent Overlay: Places an <input type="file"> with opacity: 0 on top of the "Allow" button.
    • When tapped, it counts as a legitimate user action, opening the file picker.

Implementation Details

  • Created a dialog component containing the “Allow” button.
  • Positioned a hidden file input over the button with CSS (position: absolute; opacity: 0;).
  • On tap/click, the file picker opens because the user is directly interacting with the file input.

Testing

  • iOS Safari: Confirmed that tapping the “Allow” button now successfully opens the file picker.
  • Cross-Browser: Verified that other browsers still open the file picker correctly.
  • Edge Cases: Tested multiple taps, cancellations, and reopens to ensure stability.

Screenshot

See the attached screenshot showing the dialog with the “Allow” button and the transparent file input overlay.
simulator_screenshot_34C6F370-B68C-4D79-98F6-FD6F6D9C3DA2


By merging this PR, we ensure a consistent, user-friendly file upload experience on iOS Safari, aligning with the direct interaction requirement.

@ishaquehassan ishaquehassan changed the title 11 IOS File Picker Fixed Mar 19, 2025
@ishaquehassan ishaquehassan changed the title IOS File Picker Fixed IOS Safari File Picker Fixed Mar 19, 2025
- Removed unnecessary import of `html/parser.dart`.
- Removed the `_ensureInitialized` function and the related `_kFilePickerInputsDomId`.
- Removed the private variable `_target`.
- Refactored code to remove parsing of HTML string into document object.
- Added a check for null in `_addFileToResult`
- added a null check if the file attribute is not null.
- Added a null check for the files attribute in the `pickFiles` function.
- improved conditional statement for checking isSafariIos
The confirmation modal's styles have been updated to more closely match the style of iOS dialogs. This includes changes to the modal's positioning, background color, borders, shadows, and overall width. The content container, title, detail text, and button container have all been adjusted to improve visual consistency and alignment. Button styles for the "Cancel" and "Allow" actions have also been refined. Added "Don't Allow" and "Allow" text to the buttons.
@navaronbracke
Copy link
Collaborator

This feels more like a workaround, rather than an actual fix? Instead of using a fake dialog, I would look into using a more long term solution.

@navaronbracke
Copy link
Collaborator

navaronbracke commented Mar 19, 2025

Maybe a long term solution is just to have the input element invisible? (instead of display="none")
That can be a fix in a few lines, instead of doing all these changes.

The input element is doing that here: https://github.com/miguelpruivo/flutter_file_picker/blob/master/lib/_internal/file_picker_web.dart#L67

See https://stackoverflow.com/questions/26501221/calling-click-event-of-input-doesnt-work-in-safari

@ishaquehassan
Copy link
Author

ishaquehassan commented Mar 19, 2025

I think we can calculate the element position where file picker is placed and then overlay the input element there.
The reason i did not call it a good solution is that we have to register scroll or drag events too for it. So faking the dialog is the only stable solution with minimum efforts @navaronbracke

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants