title: Xamarin Essentials MediaPicker not working on iOS
body: <p>I'm using Xamarin Essentials MediaPicker (v1.7.3) on my Xamarin Forms 5 app and it just freezes and eventually crashes the app on iOS but works perfectly fine on Android.</p>
  <p>My app uses the <code>MVVM</code> pattern so I have an <code>ImageButton</code> that's wired to a method in my view model and the code for that method looks like this:</p>
  <pre><code>async Task Add_Image_Tapped()
  {
     try
     {
        var photosPermission = await Permissions.CheckStatusAsync&lt;Permissions.Photos&gt;();
        var cameraPermission = await Permissions.CheckStatusAsync&lt;Permissions.Camera&gt;();
  
        if (photosPermission != PermissionStatus.Granted || photosPermission != PermissionStatus.Restricted)
           photosPermission = await Permissions.RequestAsync&lt;Permissions.Photos&gt;();
  
        if (cameraPermission != PermissionStatus.Granted || cameraPermission != PermissionStatus.Restricted)
           cameraPermission = await Permissions.RequestAsync&lt;Permissions.Camera&gt;();
  
        if ((photosPermission == PermissionStatus.Granted
           || photosPermission == PermissionStatus.Restricted)
           &amp;&amp; (cameraPermission == PermissionStatus.Granted
           || cameraPermission == PermissionStatus.Restricted))
           await HandleImagePicker();
     }
     catch(Exception e)
     {
        throw new Exception(e.Message);
     }
  }
  </code></pre>
  <p>And if the permissions check out fine, here's the method that handles the image pick up:</p>
  <pre><code>private async Task HandleImagePicker()
  {
     try
     {
        var filePath = string.Empty;
        var fileName = string.Empty;
  
        MainThread.BeginInvokeOnMainThread(() =&gt; {
  
           var result = MediaPicker.PickPhotoAsync(new MediaPickerOptions
           {
              Title = &quot;Pick Image&quot;
           }).Result;
  
           if (result == null)
              return;
  
           filePath = result.FullPath;
           fileName = result.FileName;
  
        });
  
        if (!string.IsNullOrEmpty(filePath) &amp;&amp; !string.IsNullOrEmpty(fileName))
        {
           // Do something with the image
        }
     }
     catch(Exception e)
     {
        throw new Exception(e.Message);
     }
  }
  </code></pre>
  <p>I'm using <code>Xamarin Forms 5.0.0.2401</code>, <code>Xamarin Community Toolkit 2.0.2</code> and <code>Xamarin Essentials 1.7.3</code>. I also have the following permissions in my <code>Info.plist</code>:</p>
  <pre><code>&lt;key&gt;NSCameraUsageDescription&lt;/key&gt;
  &lt;string&gt;MyApp would like to access your camera&lt;/string&gt;
  &lt;key&gt;NSMicrophoneUsageDescription&lt;/key&gt;
  &lt;string&gt;MyApp would like to access your microphone&lt;/string&gt;
  &lt;key&gt;NSPhotoLibraryUsageDescription&lt;/key&gt;
  &lt;string&gt;MyApp would like to access your photo library&lt;/string&gt;
  &lt;key&gt;NSPhotoLibraryAddUsageDescription&lt;/key&gt;
  &lt;string&gt;MyApp would like to access your photo library&lt;/string&gt;
  </code></pre>
  <p>I also initialize Xamarin Essentials in <code>AppDelegate</code> <code>FinishedLaunching()</code> method as below:</p>
  <pre><code>Xamarin.Essentials.Platform.Init(() =&gt; new UIViewController());
  </code></pre>
  <p>Any idea what maybe the issue here?</p>
  <p>UPDATE:</p>
  <p>The above code reflects the current code I have after making changes based on suggestions such as using the main thread.</p>
  <p>Currently, if I run the app on iOS Simulator on a Mac, it literally hangs forever. Never throws an exception or crashes.</p>
  <p>If I run it on a real device i.e. iPhone Xs, it will hang for quite a while and eventually crash the whole app.</p>
  <p>As mentioned earlier, it all works perfectly fine on Android.</p>