jamesmontemagno / MediaPlugin

Take & Pick Photos and Video Plugin for Xamarin and Windows
MIT License
712 stars 358 forks source link

Bug when invoking PickPhotoAsync from a UIAlertView click handler #186

Closed rusty21 closed 7 years ago

rusty21 commented 7 years ago

When calling TakePhotoAsync or PickPhotoAsync from inside a click hander on a UIAlertView the UI that it brings up appears where the UIAlertView was and the disappears after a second. Calling TakePhotoAsync from anywhere else seems to work fine. In the code I have here PickPhotoClick, calls PickPhotoAsync after setting some options.

vwContent.AddGestureRecognizer (new UITapGestureRecognizer ( async gesture =>
{
        // Calling this here works just fine
    // await PickPhotoClick ();

    var alert = new UIAlertView ();
    alert.Title = "Add a photo";
    alert.Message = "Choose a way to add a photo";
    alert.AddButton ("Take Photo");
    alert.AddButton ("Pick Photo");
    alert.AddButton ("Cancel");
    alert.Clicked += async (sender, e) =>
    {
        if (e.ButtonIndex == 1)
        {
                      // This does not work
                  await PickPhotoClick ();  
        }
        else if (e.ButtonIndex == 0)
        {
                      // This does not work
                  await TakePhotoClick ();
        }
    };

    alert.Show ();
}));

Version Number of Plugin: 2.6.2 Device Tested On: iPhone 6 plus Simulator Tested On: N/A

Expected Behavior

When calling TakePhotoAsync or PickPhotoAsync from a UIAlertView click handler the ui appears full screen and is usable.

Actual Behavior

Ui appears in a small box where the alert view used to be and then disappears after a second.

Steps to reproduce the Behavior

  1. Create an alert dialog where the click handler calls TakePhotoAsync or PickPhotoAsync
  2. Tap your button on the alert dialog and watch the problem occur.
modplug commented 7 years ago

I'm also experiencing this behaviour.

jamesmontemagno commented 7 years ago

You probably need to wait for the view to be fully gone as the view hierarcy probably gets messed up. you may want to add a small wait in there.

benevbright commented 7 years ago

This issue will affect to more people because XF 2.3.4-pre release changed to use UIAlertController from old one. My project also calls PickPhotoAsync from ActionSheet button event, and I updated to XF 2.3.4-pre6 and Gallery never showed up on iOS. It does not happened on XF 2.3.3.

Thankfully, James' adviced putting small delay worked for workaround.

jimmgarrido commented 7 years ago

This fix for Forms should hopefully prevent this.

jamesmontemagno commented 7 years ago

Will close as it isn't related to the library.

benevbright commented 7 years ago

@jimmgarrido 's PR is for forms. But rusty21's project is Xamarin.iOS. Then.. Is this OK to close?

jamesmontemagno commented 7 years ago

Seems like they could follow the same formula.

rusty21 commented 7 years ago

Adding the delay does work but its very undesirable. What happens when the phone lags and then the alert dialog isn't gone in time? What about if apple changes the duration of their animations to be longer? The solution fails in these scenarios.

jamesmontemagno commented 7 years ago

I'll look into it, not sure if it has something to do with the library though, but will check.

eopeter commented 7 years ago

Adding a 20, 200, 2000 millisecond did not workaround for me. Is this not the way to wait?

System.Threading.Thread.Sleep(2000); if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsPickPhotoSupported) { await page.DisplayAlert("No Camera", ":( No camera available.", "Got It"); return; }

jamesmontemagno commented 7 years ago

Work around for ignoring alertcontrollers can be found in latest beta.

rusty21 commented 7 years ago

@eopeter in this case the proper way to wait is to do await Task.Delay(500) instead of Thread.Sleep(500)