Skip to content

Commit

Permalink
Merge pull request #16 from therealjohn/fix-15
Browse files Browse the repository at this point in the history
Fix sharing on modal views for iOS
  • Loading branch information
jamesmontemagno committed Jan 26, 2016
2 parents 04c960d + 3006a09 commit 70218e8
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions Share/Share.Plugin.iOSUnified/ShareImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -91,6 +87,30 @@ public async Task ShareLink(string url, string message = null, string title = nu
Console.WriteLine("Unable to share text" + ex.Message);
}
}

/// <summary>
/// Gets the visible view controller.
/// </summary>
/// <returns>The visible view controller.</returns>
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;
}

}
}

0 comments on commit 70218e8

Please sign in to comment.