dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

iOS - MediaPicker.PickPhotoAsync() doesn't pick Photo in second attempt, if Limited permission is given to Gallery #20311

Open MGohil opened 10 months ago

MGohil commented 10 months ago

Description

When we are giving the "Limit Access" permission while picking Photos from Gallery in iOS platform, it seems to work first time, but in 2nd attempt it is not picking the single selected image.

I have already raised this as a comment on one similar issue at #14973 and "Picking photo from Gallery feature" is not yet fully working even in .NET Maui Version 8.0.6 after such a long time.

After selecting photos to allow my app to have access to, through Limit Access, it works fine first time, but after killing the app and opening again and then if we try to pick image from gallery, it keeps asking for multiple image selection which is actually an intended behaviour for giving access of selected photos to the app. But it is not showing the single image selection from my previously selected photos which my app has access to.

Following is my code to pick the file from Gallery and display it on the screen.

App.Current.Dispatcher.Dispatch(async () =>
{
    PermissionStatus status = await CheckAndRequestPhotosPermission();
    if (status == PermissionStatus.Granted || status == PermissionStatus.Limited)
    {
        var imageFile = await MediaPicker.PickPhotoAsync();
        image.Source = ImageSource.FromStream(() => imageFile.OpenReadAsync().Result);
    }
 });

public async Task<PermissionStatus> CheckAndRequestPhotosPermission()
{
    PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.Photos>();
    if (status == PermissionStatus.Granted)
    {
        return status;
    }
    if (status == PermissionStatus.Denied && DeviceInfo.Platform == DevicePlatform.iOS)
    {
        // Prompt the user to turn on in settings On iOS once a
        // permission has been denied it may not be requested again from
        // the application
        return status;
    }
    if (Permissions.ShouldShowRationale<Permissions.Photos>())
    {
    }
    status = await Permissions.RequestAsync<Permissions.Photos>();
    return status;
}

I have attached the video recording of this behaviour.

https://github.com/dotnet/maui/assets/16605284/e65f5f93-93cb-4290-bfd5-38aafb4e74a4

It would be great if Maui team fix this on priority as this is really a basic feature which app needs to have.

Thank you.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.6

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

No

Relevant log output

No response

ghost commented 10 months ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

davibittencourtome commented 8 months ago

I'm facing the same problem, when limited permission is given and the photos are selected, the next screen doesn't open, and shows the error in the output:

Attempt to present <UIImagePickerController: 0x10d824800> on <Microsoft_Maui_Platform_PageViewController: 0x12663f720> which is waiting for a delayed presentation of <UIImagePickerController: 0x1146d4800> to complete.

nethusan commented 8 months ago

I've encountered a similar issue and created a new repository with just the image upload code using the latest MAUI version, 8.0.14, but the problem persists. The same error occurs as mentioned by @davibittencourtome.

@MGohil For reference, here's my repository URL: https://github.com/nethusan/mediaPickerExample, which might be helpful for further analysis or if anyone wants to use it as a reference for reproducing the issue. (@MGohil I used your code above in this repo, hope that was ok)

This issue prevent us going to production right now, I hope this can be fixed asap, since this is basic functionality.

kimhongka commented 8 months ago

Im also getting the same issue with MAUI MediaPicker.PickPhotoAsync() on iOS. Only it works with Full access permission.

alzubitariq commented 7 months ago

Any updates ?

fusionconsulting commented 7 months ago

Seeing this issue here and it's holding back a release. Any updates?

gautambjain commented 6 months ago

Please. This is a very common functionality and should be given a very high priority. Please fix.

MGohil commented 6 months ago

@jfversluis May I please request to move this to a priority bug? This is really holding us back since quite a long time now. I hope this issue will soon be addressed by Microsoft.

Thank you for your continuous support.

rdurish commented 6 months ago

Same here issue here, this issue definitely hurts/hinders the app experience.

letscodewithkalyan commented 6 months ago

Any Updated Maui Team ?

ThinhKVT commented 5 months ago

We encountered the same issue on our MAUI project on IOS. Have any solution for the problem ?

samhouts commented 5 months ago

We agree that this is an important issue. As our roadmap indicates, for the near future, we are focused on:

I am marking this as a p/2 issue because it does not meet our focus areas right now, but we are constantly revisiting the backlog for important issues to address as time allows. Thank you for your patience and understanding!

kimhongka commented 5 months ago

I ended up added another compromised workaround solution that showing a prompt to ask user to manually change the permisstion to full if the detected permission is Limted one.. sad, another workaround.

There are too many bugs... only about 50 bug fixes per month. At this speed, it will take another 2 years to be stable. I cannot believe this is considered a production-ready product and killed XF without a plan.

It's been more than 2 years trying to make the CollectionView work, and the priority is still said to be fixing the CollectionView and its layout.

We have converted a few XF apps to MAUI apps so far, with a LOT of workarounds. If we keep going "workaround" and around in this cycle, we will be stuck in an infinite bug loop.

We found another bug on the Tabbed Page where it selects the wrong tab at inital loading in Android when select 4th tab directly, out of 5 tabs if it has some controls like stacklayout etc. We are at a stage where we don’t even bother to report a bug anymore since only an AI bot seems to reply or park it to backlog. We have 3 more apps to convert to MAUI.

Please, please make it work. Don't add any new features or Don't mention about Blazor or Don't make a youtube that PM said MAUI is great, we all know it is not yet there and face the reality. Just make MAUI at least as stable as XF.

Why are we going back to the early XF era when it was introduced from Xamarin Native? It was the era of bugs... we have been there, right?

I appreciate your hard work. The new change is better than a stagnation with old version. Water that doesn't flow becomes stagnant; change is essential to survive, we know that as developer. Yet, it is really painful to use a half-baked product.

Sorry to bring up this negativness. I'm really trying to believe Maui is going to be better. Thank you, please take a look...

khalil2099 commented 5 months ago

I ended up added another compromised workaround solution that showing a prompt to ask user to manually change the permisstion to full if the detected permission is Limted one.. sad, another workaround.

There are too many bugs... only about 50 bug fixes per month. At this speed, it will take another 2 years to be stable. I cannot believe this is considered a production-ready product and killed XF without a plan.

It's been more than 2 years trying to make the CollectionView work, and the priority is still said to be fixing the CollectionView and its layout.

We have converted a few XF apps to MAUI apps so far, with a LOT of workarounds. If we keep going "workaround" and around in this cycle, we will be stuck in an infinite bug loop.

We found another bug on the Tabbed Page where it selects the wrong tab at inital loading in Android when select 4th tab directly, out of 5 tabs if it has some controls like stacklayout etc. We are at a stage where we don’t even bother to report a bug anymore since only an AI bot seems to reply or park it to backlog. We have 3 more apps to convert to MAUI.

Please, please make it work. Don't add any new features or Don't mention about Blazor or Don't make a youtube that PM said MAUI is great, we all know it is not yet there and face the reality. Just make MAUI at least as stable as XF.

Why are we going back to the early XF era when it was introduced from Xamarin Native? It was the era of bugs... we have been there, right?

I appreciate your hard work. The new change is better than a stagnation with old version. Water that doesn't flow becomes stagnant; change is essential to survive, we know that as developer. Yet, it is really painful to use a half-baked product.

Sorry to bring up this negativness. I'm really trying to believe Maui is going to be better. Thank you, please take a look...

no other workaround other than asking the user to give full permission ?

rdurish commented 5 months ago

I ended up added another compromised workaround solution that showing a prompt to ask user to manually change the permisstion to full if the detected permission is Limted one.. sad, another workaround. There are too many bugs... only about 50 bug fixes per month. At this speed, it will take another 2 years to be stable. I cannot believe this is considered a production-ready product and killed XF without a plan. It's been more than 2 years trying to make the CollectionView work, and the priority is still said to be fixing the CollectionView and its layout. We have converted a few XF apps to MAUI apps so far, with a LOT of workarounds. If we keep going "workaround" and around in this cycle, we will be stuck in an infinite bug loop. We found another bug on the Tabbed Page where it selects the wrong tab at inital loading in Android when select 4th tab directly, out of 5 tabs if it has some controls like stacklayout etc. We are at a stage where we don’t even bother to report a bug anymore since only an AI bot seems to reply or park it to backlog. We have 3 more apps to convert to MAUI. Please, please make it work. Don't add any new features or Don't mention about Blazor or Don't make a youtube that PM said MAUI is great, we all know it is not yet there and face the reality. Just make MAUI at least as stable as XF. Why are we going back to the early XF era when it was introduced from Xamarin Native? It was the era of bugs... we have been there, right? I appreciate your hard work. The new change is better than a stagnation with old version. Water that doesn't flow becomes stagnant; change is essential to survive, we know that as developer. Yet, it is really painful to use a half-baked product. Sorry to bring up this negativness. I'm really trying to believe Maui is going to be better. Thank you, please take a look...

no other workaround other than asking the user to give full permission ?

Would love to see your solution if you're willing to share with the community.

kimhongka commented 4 months ago

No fix, needed to ask user to go to app setting and manually change the permission. I'm so embarrassed on this for my client and users. It is not professional at all.

davidschachtschabel commented 4 months ago

I am currently migrating a Xamarin.Forms app to .NET MAUI and need this feature, because it is a main function of the app. Is there a new status?

AndreKraemer commented 4 months ago

@samhouts @jfversluis this Bug is currently blocking us in a Xamarin.Forms to .NET MAUI migration. I assume we're no the only ones. Any chance to give it a higher priority?

knasher commented 3 months ago

This is also preventing me from launching our MVP, this really needs to be fixed ASAP

knasher commented 3 months ago

I've found removing this line status = await Permissions.RequestAsync<Permissions.Photos>(); helps a little bit as is lets you select a photo but also sometimes pops up with a dialog asking the user if they want to change which photos the app can access (which doesn't make any difference). Still not a great experience though

knasher commented 3 months ago

Actually, that doesn't work as well as expected, this is still pretty broken... Anyone know of any other media picker solutions out there? I can't wait for this to be fixed.

Kinda sad MAUI isn't ready

fusionconsulting commented 3 months ago

I've been moving an app over to Maui for the last few months and it's been nothing but problem after problem. Sad that it's nowhere near ready and we're forced to use it. I've made the decision to move to Dart/Flutter and it's been very refreshing to see everything work as it should. Hope Maui catches up one day.

Serebie commented 3 months ago

Perhaps this happens since the permissions system is using the newer PhotoKit library while the MediaPicker is still using the UIImagePickerController in iOS, but I did not look deeper into it.

For anyone still looking for a solution, I am using something similar to the sample in the following repository since the Xamarin days which works quite nice with Maui as well.

Workaround repository

In this workaround I am not asking for the users permission, since the app does not need at as stated in the iOS Documentation for the PhotoKit.

Displaying the photo library doesn’t need user permission because it’s running in a separate process. An app can’t take screenshots of content and can only read the assets that the user selects. (Selecting photos and videos in iOS)

If you want to use the limited photo library, you'll need ask for the authorization status and act accordingly.

I Hope this helps some of you.

AndreKraemer commented 3 months ago

Thank you @Serebie!

Serebie commented 3 months ago

I had another look at the MediaPicker. Besides using the old UIImagePickerController instead of the more recent PHPickerViewCotroller, there seems to be no issue with it on iOS as long as you do not call Permissions.RequestAsync<Permissions.Photos>().

Since the displayed photo library is off-process, your app can only fetch the pictures selected by the user. Apple is fine with you not asking the user for permission to access the photo library.

In case you still want to request the users permissions, make sure to call Permissions.RequestAsync<Permissions.Photos>() only one time per installation. If the user selects "Limited Access...", this method will open the limited library picker, where the user can create a limited library for the app. Every further call will execute the following block and show the limited library again. (This wasn't the case with Xamarin)

else if (OperatingSystem.IsIOSVersionAtLeast(14) && status == PermissionStatus.Limited)
{
    PhotosUI.PHPhotoLibrary_PhotosUISupport.PresentLimitedLibraryPicker(
        PHPhotoLibrary.SharedPhotoLibrary,
        WindowStateManager.Default.GetCurrentUIViewController());
    return status;
}

Since this call is not awaited or anything, calling await MediaPicker.PickPhotoAsync() directly afterwards will lead to a problem since a photo library picker has already been opened. Therefore the actual photo picker won't be displayed (and also never disposed).

To prevent the dialog mentioned by @knasher from being displayed add this to your Info.plist.

<key>PHPhotoLibraryPreventAutomaticLimitedAccessAlert</key>
<true/>

The default behaviour for the limited library picker is to be diplayed once per app life. This entry prevents this.

If you want to use the MediaPicker, I would suggest to just ignore the permissions on iOS. The Limited Library feature from the iOS PhotoKit isn't supported by the MediaPicker anyway. The user can create a limited library, but the necessary delegates for handling this are not implemented and in the end you will open the library containing all images. If you have a use case for the limited library (like browsing, editing, creating backups, etc.), you will need to implement it yourself.

AndreKraemer commented 3 months ago

Thanks a ton, @Serebie, for your thorough analysis and clear explanation! We tested your workaround posted in https://github.com/dotnet/maui/issues/20311#issuecomment-2304122166 (https://github.com/Serebie/Maui_20311_Workaround), and it works perfectly!

Also, a big thanks for highlighting that iOS doesn’t require explicit permission requests, and avoiding Permissions.RequestAsync<Permissions.Photos>() indeed resolves the issue!

Odaronil commented 1 month ago

6903