diff --git a/Share/Share.Plugin.iOSUnified/ShareImplementation.cs b/Share/Share.Plugin.iOSUnified/ShareImplementation.cs index 33a3379..6544a15 100644 --- a/Share/Share.Plugin.iOSUnified/ShareImplementation.cs +++ b/Share/Share.Plugin.iOSUnified/ShareImplementation.cs @@ -45,14 +45,12 @@ public async Task Share(string text, string title = null) { var items = new NSObject[] { new NSString(text ?? string.Empty) }; var activityController = new UIActivityViewController(items, null); + var vc = GetVisibleViewController(); + if (activityController.PopoverPresentationController != null) { - activityController.PopoverPresentationController.SourceView = - UIApplication.SharedApplication.KeyWindow.RootViewController.ChildViewControllers != null - ? UIApplication.SharedApplication.KeyWindow.RootViewController.ChildViewControllers[0].View - : UIApplication.SharedApplication.KeyWindow.RootViewController.View; + activityController.PopoverPresentationController.SourceView = vc.View; } - var vc = UIApplication.SharedApplication.KeyWindow.RootViewController; await vc.PresentViewControllerAsync(activityController, true); } @@ -75,14 +73,12 @@ public async Task ShareLink(string url, string message = null, string title = nu { var items = new NSObject[] { new NSString(message ?? string.Empty), NSUrl.FromString(url) }; var activityController = new UIActivityViewController(items, null); + var vc = GetVisibleViewController(); + if (activityController.PopoverPresentationController != null) { - activityController.PopoverPresentationController.SourceView = - UIApplication.SharedApplication.KeyWindow.RootViewController.ChildViewControllers != null - ? UIApplication.SharedApplication.KeyWindow.RootViewController.ChildViewControllers[0].View - : UIApplication.SharedApplication.KeyWindow.RootViewController.View; + activityController.PopoverPresentationController.SourceView = vc.View; } - var vc = UIApplication.SharedApplication.KeyWindow.RootViewController; await vc.PresentViewControllerAsync(activityController, true); } @@ -91,6 +87,30 @@ public async Task ShareLink(string url, string message = null, string title = nu Console.WriteLine("Unable to share text" + ex.Message); } } + + /// + /// Gets the visible view controller. + /// + /// The visible view controller. + UIViewController GetVisibleViewController() + { + var rootController = UIApplication.SharedApplication.KeyWindow.RootViewController; + + if (rootController.PresentedViewController == null) + return rootController; + + if (rootController.PresentedViewController is UINavigationController) + { + return ((UINavigationController)rootController.PresentedViewController).TopViewController; + } + + if (rootController.PresentedViewController is UITabBarController) + { + return ((UITabBarController)rootController.PresentedViewController).SelectedViewController; + } + + return rootController.PresentedViewController; + } } } \ No newline at end of file