diff --git a/Xamarin.Essentials/Contacts/Contacts.ios.macos.cs b/Xamarin.Essentials/Contacts/Contacts.ios.macos.cs index d1b54132e..17022bb04 100644 --- a/Xamarin.Essentials/Contacts/Contacts.ios.macos.cs +++ b/Xamarin.Essentials/Contacts/Contacts.ios.macos.cs @@ -39,6 +39,14 @@ static Task PlatformPickContactAsync() }) }; + if (picker.PresentationController != null) + { + picker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate + { + DismissHandler = () => source?.TrySetResult(null) + }; + } + uiView.PresentViewController(picker, true, null); return source.Task; diff --git a/Xamarin.Essentials/Email/Email.ios.cs b/Xamarin.Essentials/Email/Email.ios.cs index 0415350cb..5b0cf6613 100644 --- a/Xamarin.Essentials/Email/Email.ios.cs +++ b/Xamarin.Essentials/Email/Email.ios.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Threading.Tasks; using Foundation; using MessageUI; @@ -50,13 +51,21 @@ static Task ComposeWithMailCompose(EmailMessage message) } } - // show the controller var tcs = new TaskCompletionSource(); controller.Finished += (sender, e) => { controller.DismissViewController(true, null); tcs.TrySetResult(e.Result == MFMailComposeResult.Sent); }; + + if (controller.PresentationController != null) + { + controller.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate + { + DismissHandler = () => tcs.TrySetResult(false) + }; + } + parentController.PresentViewController(controller, true, null); return tcs.Task; diff --git a/Xamarin.Essentials/FilePicker/FilePicker.ios.cs b/Xamarin.Essentials/FilePicker/FilePicker.ios.cs index 284829ddc..76516c429 100644 --- a/Xamarin.Essentials/FilePicker/FilePicker.ios.cs +++ b/Xamarin.Essentials/FilePicker/FilePicker.ios.cs @@ -34,9 +34,9 @@ static async Task> PlatformPickAsync(PickOptions options if (documentPicker.PresentationController != null) { - documentPicker.PresentationController.Delegate = new PickerPresentationControllerDelegate + documentPicker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate { - PickHandler = urls => GetFileResults(urls, tcs) + DismissHandler = () => GetFileResults(null, tcs) }; } @@ -74,14 +74,6 @@ public override void DidPickDocument(UIDocumentPickerViewController controller, public override void DidPickDocument(UIDocumentPickerViewController controller, NSUrl url) => PickHandler?.Invoke(new NSUrl[] { url }); } - - class PickerPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate - { - public Action PickHandler { get; set; } - - public override void DidDismiss(UIPresentationController presentationController) => - PickHandler?.Invoke(null); - } } public partial class FilePickerFileType diff --git a/Xamarin.Essentials/MediaPicker/MediaPicker.ios.cs b/Xamarin.Essentials/MediaPicker/MediaPicker.ios.cs index 6c38e5682..44a06155a 100644 --- a/Xamarin.Essentials/MediaPicker/MediaPicker.ios.cs +++ b/Xamarin.Essentials/MediaPicker/MediaPicker.ios.cs @@ -75,9 +75,9 @@ static async Task PhotoAsync(MediaPickerOptions options, bool photo, if (picker.PresentationController != null) { - picker.PresentationController.Delegate = new PhotoPickerPresentationControllerDelegate + picker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate { - CompletedHandler = info => GetFileResult(info, tcs) + DismissHandler = () => GetFileResult(null, tcs) }; } @@ -167,13 +167,5 @@ public override void FinishedPickingMedia(UIImagePickerController picker, NSDict public override void Canceled(UIImagePickerController picker) => CompletedHandler?.Invoke(null); } - - class PhotoPickerPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate - { - public Action CompletedHandler { get; set; } - - public override void DidDismiss(UIPresentationController presentationController) => - CompletedHandler?.Invoke(null); - } } } diff --git a/Xamarin.Essentials/Platform/Platform.ios.tvos.watchos.cs b/Xamarin.Essentials/Platform/Platform.ios.tvos.watchos.cs index 635e4d3f7..3461d6e6d 100644 --- a/Xamarin.Essentials/Platform/Platform.ios.tvos.watchos.cs +++ b/Xamarin.Essentials/Platform/Platform.ios.tvos.watchos.cs @@ -73,12 +73,20 @@ internal static bool HasOSVersion(int major, int minor) => #if __IOS__ || __TVOS__ + static Func getCurrentController; + + public static void Init(Func getCurrentUIViewController) + => getCurrentController = getCurrentUIViewController; + public static UIViewController GetCurrentUIViewController() => GetCurrentViewController(false); internal static UIViewController GetCurrentViewController(bool throwIfNull = true) { - UIViewController viewController = null; + var viewController = getCurrentController?.Invoke(); + + if (viewController != null) + return viewController; var window = UIApplication.SharedApplication.KeyWindow; @@ -138,5 +146,24 @@ internal static UIWindow GetCurrentWindow(bool throwIfNull = true) internal static NSOperationQueue GetCurrentQueue() => NSOperationQueue.CurrentQueue ?? new NSOperationQueue(); + +#if __IOS__ + internal class UIPresentationControllerDelegate : UIAdaptivePresentationControllerDelegate + { + public Action DismissHandler { get; set; } + + public override void DidDismiss(UIPresentationController presentationController) + { + DismissHandler?.Invoke(); + DismissHandler = null; + } + + protected override void Dispose(bool disposing) + { + DismissHandler?.Invoke(); + base.Dispose(disposing); + } + } +#endif } } diff --git a/Xamarin.Essentials/Sms/Sms.ios.cs b/Xamarin.Essentials/Sms/Sms.ios.cs index 19da4bbb1..acd775f99 100644 --- a/Xamarin.Essentials/Sms/Sms.ios.cs +++ b/Xamarin.Essentials/Sms/Sms.ios.cs @@ -21,13 +21,21 @@ static Task PlatformComposeAsync(SmsMessage message) messageController.Recipients = message?.Recipients?.ToArray() ?? new string[] { }; - // show the controller var tcs = new TaskCompletionSource(); messageController.Finished += (sender, e) => { messageController.DismissViewController(true, null); tcs?.TrySetResult(e.Result == MessageComposeResult.Sent); }; + + if (controller.PresentationController != null) + { + controller.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate + { + DismissHandler = () => tcs.TrySetResult(false) + }; + } + controller.PresentViewController(messageController, true, null); return tcs.Task;