cjlotz / Xamarin.Plugins

Cross platform Xamarin Plugins
MIT License
113 stars 56 forks source link

PCL code works for Droid but not iOS #72

Closed WillAutioItrax closed 7 years ago

WillAutioItrax commented 7 years ago

Hi. I am developing in Xamarin Forms, VS2017, a PCL project. The following code works fine on Droid:

` var emailMessenger = CrossMessaging.Current.EmailMessenger;

    if (emailMessenger.CanSendEmail)
    {
    string subject =  "Some Subject";
    var email = new EmailMessageBuilder()
      .To("my.name@my.company.com")
      .Subject(subject)
      .Body("Content for body")
      .Build();
emailMessenger.SendEmail(email);
}`

It opens an email client and pre-populates the desired items.

However on iOS, by executing the same PCL code I do not get an email client opening. I do get a warning in Output which I suspect indicates an issue.

Warning: Attempt to present <MFMailComposeViewController: 0x1710b200> on <Xamarin_Forms_Platform_iOS_PageRenderer: 0x17a2e820> whose view is not in the window hierarchy!

Any direction on how to fix this is appreciated.

madareklaw commented 7 years ago

I have the exact same issue, works in android, but not IOS

            var messenger = CrossMessaging.Current.EmailMessenger;
            if (messenger.CanSendEmailAttachments)
            {
                var email = new EmailMessageBuilder()
                .To("")
                .Subject("")
                .BodyAsHtml("")
                .WithAttachment(filePath, "image/jpeg")
                .Build();

                messenger.SendEmail(email);
            }

I get this error:

Warning: Attempt to present <MFMailComposeViewController: 0x104c50800> on <Xamarin_Forms_Platform_iOS_PageRenderer: 0x103d45c50> whose view is not in the window hierarchy!

madareklaw commented 7 years ago

Ok, i found the solution. in Xamarin.Plugins/Messaging/Plugin.Messaging.iOSUnified/MessagingExtensions.cs there is a method called GetVisibleViewController ,all i did was replace the code that was there with this:

    private static UIViewController GetVisibleViewController(UIViewController controller)
    {           
        controller = controller ?? UIApplication.SharedApplication.KeyWindow.RootViewController;

        if (controller.PresentedViewController == null)
            return controller;

        if (controller.PresentedViewController is UINavigationController)
        {
            return ((UINavigationController)controller.PresentedViewController).VisibleViewController;
        }

        if (controller.PresentedViewController is UITabBarController)
        {
            return ((UITabBarController)controller.PresentedViewController).SelectedViewController;
        }

        return GetVisibleViewController(controller.PresentedViewController);
    }

And now it works fine :) ref: https://stackoverflow.com/questions/41241508/xamarin-forms-warning-attempt-to-present-on-whose-view-is-not-in-the-window

cjlotz commented 7 years ago

@madareklaw Thanks for the investigation and fix. I'll have a look at including this in the upcoming v5 release that I hope to publish sometime this week-end

madareklaw commented 7 years ago

@cjlotz No probs

WillAutioItrax commented 7 years ago

Awesome! Thanks you guys!

cjlotz commented 7 years ago

I ended up using some code similar to what @jamesmontemagno was using in his Media plugin to resolve the view controller. From my testing it seems to work fine. Let me know if you run into further issues with this.

kartiksolanki commented 7 years ago

@madareklaw @cjlotz : How do I make changes suggested by @madareklaw in MessagingExtension.cs file and build a plugin file for my Xamarin.Forms project? Can you please help me. Thank you.

cjlotz commented 7 years ago

@kartiksolanki Upgrade to v5 of the plugin. The fixes are included in there

kartiksolanki commented 7 years ago

@cjlotz : Thank you :)

WillAutioItrax commented 7 years ago

Did the upgrade - works like a charm. This is awesome! Thanks!