Home > Enterprise >  Xamarin Essentials MediaPicker not working on iOS
Xamarin Essentials MediaPicker not working on iOS

Time:05-11

I'm using Xamarin Essentials MediaPicker on my Xamarin Forms 5 app and it just doesn't do anything on iOS but works perfectly fine on Android.

My app uses the MVVM pattern so I have an ImageButton that's wired to a method in my view model and the code for that method looks like this:

async Task Image_Picker_Tapped()
{
   var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
   {
      Title = "Pick Image"
   };

   // Handle the image
}

I put break points to see what's happening because I get no errors at all. So, I do hit the first line where I call the MediaPicker.PickPhotoAsync() method but I don't hit my second break point. So, basically, it goes nowhere!

I'm on a PC and my iPhone Xs is connected via USB for Hot Reload. I also packaged up the app and uploaded for TestFlight testing. Again, on my iPhone Xs, it just doesn't do anything at all.

I'm using Xamarin Forms 5.0.0.2401, Xamarin Community Toolkit 2.0.2 and Xamarin Essentials 1.7.3. I also have the following permissions in my Info.plist:

<key>NSCameraUsageDescription</key>
<string>MyApp would like to access your camera</string>
<key>NSMicrophoneUsageDescription</key>
<string>MyApp would like to access your microphone</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>MyApp would like to access your photo library</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>MyApp would like to access your photo library</string>

I also initialize Xamarin Essentials in AppDelegate FinishedLaunching() method as below:

Xamarin.Essentials.Platform.Init(() => new UIViewController());

Any idea what maybe the issue here?

UPDATE:

I tried running it on MainThread and I'm getting the same results. Here's the method in my view model that gets invoked when user taps the ImageButton to pick an image:

async Task Image_Picker_Tapped()
{
   MainThread.BeginInvokeOnMainThread(() => {

      var result = MediaPicker.PickPhotoAsync(new MediaPickerOptions
      {
         Title = "Pick Image"
      }).Result;

      if (result != null)
      {
         filePath = result.FullPath;
         fileName = result.FileName;
       }
   });
}

CodePudding user response:

Can you show how you are requesting for user permissions to access the device storage.

Suggestion - add a check method for permissions check for every time you try to access the device storage. You can use Permissions.CrossPermissions plugin for it.

An sample for it would look like -

async Task Image_Picker_Tapped()
{
   if (await GetPhotoLibraryPermission())
   {
      var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
      {
         Title = "Pick Image"
      };
   }
   // Handle the image
}
private async Task<bool> GetPhotoLibraryPermission()
        {
            PermissionStatus cameraPermissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync<CameraPermission>();
            if (cameraPermissionStatus != PermissionStatus.Granted)
            {
                cameraPermissionStatus = await CrossPermissions.Current.RequestPermissionAsync<CameraPermission>();
            }
            var storagePermissionStatus = await CrossPermissions.Current.RequestPermissionAsync<StoragePermission>();
            if (storagePermissionStatus != PermissionStatus.Granted)
            {
                storagePermissionStatus = await CrossPermissions.Current.RequestPermissionAsync<StoragePermission>();
            }
            return cameraPermissionStatus == PermissionStatus.Granted && storagePermissionStatus == PermissionStatus.Granted;
        }

CodePudding user response:

Here are some suggestions

  1. Try to revert the method signature to the original one .

    private async void Image_Picker_Tapped(object sender, EventArgs e){}
    
  2. Wrap your code with Try Catch to see if there is any error information.

  3. Do not set any parameter while calling PickPhotoAsync .

    var result = await MediaPicker.PickPhotoAsync();
    
  4. Try to add await Task.Delay(100) before calling PickPhotoAsync.

  • Related