Ejemplo n.º 1
0
        static Task PlatformComposeAsync(SmsMessage message)
        {
            // do this first so we can throw as early as possible
            var controller = Platform.GetCurrentViewController();

            // create the controller
            var messageController = new MFMessageComposeViewController();

            if (!string.IsNullOrWhiteSpace(message?.Body))
            {
                messageController.Body = message.Body;
            }

            messageController.Recipients = message?.Recipients?.ToArray() ?? new string[] { };

            // show the controller
            var tcs = new TaskCompletionSource <bool>();

            messageController.Finished += (sender, e) =>
            {
                messageController.DismissViewController(true, null);
                tcs.SetResult(e.Result == MessageComposeResult.Sent);
            };
            controller.PresentViewController(messageController, true, null);

            return(tcs.Task);
        }
Ejemplo n.º 2
0
        static async Task <bool> PlatformOpenAsync(Uri uri, BrowserLaunchOptions options)
        {
            var nativeUrl = new NSUrl(uri.AbsoluteUri);

            switch (options.LaunchMode)
            {
            case BrowserLaunchMode.SystemPreferred:
                var sfViewController = new SFSafariViewController(nativeUrl, false);
                var vc = Platform.GetCurrentViewController();

                if (options.PreferredToolbarColor.HasValue)
                {
                    sfViewController.PreferredBarTintColor = options.PreferredToolbarColor.Value.ToPlatformColor();
                }

                if (options.PreferredControlColor.HasValue)
                {
                    sfViewController.PreferredControlTintColor = options.PreferredControlColor.Value.ToPlatformColor();
                }

                if (sfViewController.PopoverPresentationController != null)
                {
                    sfViewController.PopoverPresentationController.SourceView = vc.View;
                }
                await vc.PresentViewControllerAsync(sfViewController, true);

                break;

            case BrowserLaunchMode.External:
                return(await UIApplication.SharedApplication.OpenUrlAsync(nativeUrl, new UIApplicationOpenUrlOptions()));
            }

            return(true);
        }
Ejemplo n.º 3
0
        static Task PlatformRequestAsync(ShareMultipleFilesRequest request)
        {
            var items = new List <NSObject>();

            var hasTitel = !string.IsNullOrWhiteSpace(request.Title);

            foreach (var file in request.Files)
            {
                var fileUrl = NSUrl.FromFilename(file.FullPath);
                if (hasTitel)
                {
                    items.Add(new ShareActivityItemSource(fileUrl, request.Title)); // Share with title (subject)
                }
                else
                {
                    items.Add(fileUrl); // No title specified
                }
            }

            var activityController = new UIActivityViewController(items.ToArray(), null);

            var vc = Platform.GetCurrentViewController();

            if (activityController.PopoverPresentationController != null)
            {
                activityController.PopoverPresentationController.SourceView = vc.View;

                if (request.PresentationSourceBounds != Rectangle.Empty || Platform.HasOSVersion(13, 0))
                {
                    activityController.PopoverPresentationController.SourceRect = request.PresentationSourceBounds.ToPlatformRectangle();
                }
            }

            return(vc.PresentViewControllerAsync(activityController, true));
        }
Ejemplo n.º 4
0
        static Task PlatformRequestAsync(ShareFileRequest request)
        {
            var items = new List <NSObject>();

            var fileUrl = NSUrl.FromFilename(request.File.FullPath);

            if (!string.IsNullOrEmpty(request.Title))
            {
                items.Add(new ShareActivityItemSource(fileUrl, request.Title)); // Share with title (subject)
            }
            else
            {
                items.Add(fileUrl); // No title specified
            }
            var activityController = new UIActivityViewController(items.ToArray(), null);

            var vc = Platform.GetCurrentViewController();

            if (activityController.PopoverPresentationController != null)
            {
                activityController.PopoverPresentationController.SourceView = vc.View;
            }

            return(vc.PresentViewControllerAsync(activityController, true));
        }
Ejemplo n.º 5
0
        public static Task OpenAsync(Uri uri, BrowserLaunchType launchType)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            var nativeUrl = new NSUrl(uri.OriginalString);

            switch (launchType)
            {
            case BrowserLaunchType.SystemPreferred:
                var sfViewController = new SFSafariViewController(nativeUrl, false);
                var vc = Platform.GetCurrentViewController();

                if (sfViewController.PopoverPresentationController != null)
                {
                    sfViewController.PopoverPresentationController.SourceView = vc.View;
                }
                vc.PresentViewController(sfViewController, true, null);
                break;

            case BrowserLaunchType.External:
                UIKit.UIApplication.SharedApplication.OpenUrl(nativeUrl);
                break;
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 6
0
        static Task PlatformRequestAsync(ShareTextRequest request)
        {
            var items = new List <NSObject>();

            if (!string.IsNullOrWhiteSpace(request.Text))
            {
                items.Add(new ShareActivityItemSource(new NSString(request.Text), request.Title));
            }

            if (!string.IsNullOrWhiteSpace(request.Uri))
            {
                items.Add(new ShareActivityItemSource(NSUrl.FromString(request.Uri), request.Title));
            }

            var activityController = new UIActivityViewController(items.ToArray(), null);

            var vc = Platform.GetCurrentViewController();

            if (activityController.PopoverPresentationController != null)
            {
                activityController.PopoverPresentationController.SourceView = vc.View;
                if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
                {
                    var sr = new CoreGraphics.CGRect(vc.View.Bounds.Width, 20, 0, 0);
                    activityController.PopoverPresentationController.SourceRect = sr;
                }
            }

            return(vc.PresentViewControllerAsync(activityController, true));
        }
Ejemplo n.º 7
0
        static Task PlatformRequestAsync(ShareFileRequest request)
        {
            var items = new List <NSObject>();

            var fileUrl = NSUrl.FromFilename(request.File.FullPath);

            if (!string.IsNullOrEmpty(request.Title))
            {
                items.Add(new ShareActivityItemSource(fileUrl, request.Title)); // Share with title (subject)
            }
            else
            {
                items.Add(fileUrl); // No title specified
            }
            var activityController = new UIActivityViewController(items.ToArray(), null);

            var vc = Platform.GetCurrentViewController();

            if (activityController.PopoverPresentationController != null)
            {
                activityController.PopoverPresentationController.SourceView = vc.View;
                if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
                {
                    var sr = new CoreGraphics.CGRect(vc.View.Bounds.Width, 20, 0, 0);
                    activityController.PopoverPresentationController.SourceRect = sr;
                }
            }

            return(vc.PresentViewControllerAsync(activityController, true));
        }
Ejemplo n.º 8
0
        static Task <Contact> PlatformPickContactAsync()
        {
            var uiView = Platform.GetCurrentViewController();

            if (uiView == null)
            {
                throw new ArgumentNullException($"The View Controller can't be null.");
            }

            var source = new TaskCompletionSource <Contact>();

            var picker = new CNContactPickerViewController
            {
                Delegate = new ContactPickerDelegate(phoneContact =>
                {
                    try
                    {
                        source?.TrySetResult(ConvertContact(phoneContact));
                    }
                    catch (Exception ex)
                    {
                        source?.TrySetException(ex);
                    }
                })
            };

            uiView.PresentViewController(picker, true, null);

            return(source.Task);
        }
Ejemplo n.º 9
0
        static Task PlatformOpenAsync(OpenFileRequest request)
        {
            var fileUrl = NSUrl.FromFilename(request.File.FullPath);

            documentController          = UIDocumentInteractionController.FromUrl(fileUrl);
            documentController.Delegate = new DocumentControllerDelegate
            {
                DismissHandler = () =>
                {
                    documentController?.Dispose();
                    documentController = null;
                }
            };
            documentController.Uti = request.File.ContentType;

            var vc = Platform.GetCurrentViewController();

            CoreGraphics.CGRect?rect = null;
            if (DeviceInfo.Idiom == DeviceIdiom.Tablet)
            {
                rect = new CoreGraphics.CGRect(new CoreGraphics.CGPoint(vc.View.Bounds.Width / 2, vc.View.Bounds.Height), CoreGraphics.CGRect.Empty.Size);
            }
            else
            {
                rect = vc.View.Bounds;
            }

            documentController.PresentOpenInMenu(rect.Value, vc.View, true);

            return(Task.CompletedTask);
        }
Ejemplo n.º 10
0
        static Task PlatformRequestAsync(ShareTextRequest request)
        {
            var items = new List <NSObject>();

            if (!string.IsNullOrWhiteSpace(request.Text))
            {
                items.Add(new ShareActivityItemSource(new NSString(request.Text), request.Title));
            }

            if (!string.IsNullOrWhiteSpace(request.Uri))
            {
                items.Add(new ShareActivityItemSource(NSUrl.FromString(request.Uri), request.Title));
            }

            var activityController = new UIActivityViewController(items.ToArray(), null);

            var vc = Platform.GetCurrentViewController();

            if (activityController.PopoverPresentationController != null)
            {
                activityController.PopoverPresentationController.SourceView = vc.View;
            }

            return(vc.PresentViewControllerAsync(activityController, true));
        }
Ejemplo n.º 11
0
        static Task ComposeWithMailCompose(EmailMessage message)
        {
            // do this first so we can throw as early as possible
            var parentController = Platform.GetCurrentViewController();

            // create the controller
            var controller = new MFMailComposeViewController();

            if (!string.IsNullOrEmpty(message?.Body))
            {
                controller.SetMessageBody(message.Body, message.BodyFormat == EmailBodyFormat.Html);
            }
            if (!string.IsNullOrEmpty(message?.Subject))
            {
                controller.SetSubject(message.Subject);
            }
            if (message?.To?.Count > 0)
            {
                controller.SetToRecipients(message.To.ToArray());
            }
            if (message?.Cc?.Count > 0)
            {
                controller.SetCcRecipients(message.Cc.ToArray());
            }
            if (message?.Bcc?.Count > 0)
            {
                controller.SetBccRecipients(message.Bcc.ToArray());
            }

            if (message?.Attachments?.Count > 0)
            {
                foreach (var attachment in message.Attachments)
                {
                    var data = NSData.FromFile(attachment.FullPath);
                    if (data == null)
                    {
                        throw new FileNotFoundException($"Attachment {attachment.FileName} not found.", attachment.FullPath);
                    }

                    controller.AddAttachmentData(data, attachment.ContentType, attachment.FileName);
                }
            }

            // show the controller
            var tcs = new TaskCompletionSource <bool>();

            controller.Finished += (sender, e) =>
            {
                controller.DismissViewController(true, null);
                tcs.TrySetResult(e.Result == MFMailComposeResult.Sent);
            };
            parentController.PresentViewController(controller, true, null);

            return(tcs.Task);
        }
Ejemplo n.º 12
0
        static Task <IEnumerable <FileResult> > PlatformPickAsync(PickOptions options, bool allowMultiple = false)
        {
            if (allowMultiple && !Platform.HasOSVersion(11, 0))
            {
                throw new FeatureNotSupportedException("Multiple file picking is only available on iOS 11 or later.");
            }

            var allowedUtis = options?.FileTypes?.Value?.ToArray() ?? new string[]
            {
                UTType.Content,
                UTType.Item,
                "public.data"
            };

            var tcs = new TaskCompletionSource <IEnumerable <FileResult> >();

            // Note: Importing (UIDocumentPickerMode.Import) makes a local copy of the document,
            // while opening (UIDocumentPickerMode.Open) opens the document directly. We do the
            // latter, so the user accesses the original file.
            var documentPicker = new UIDocumentPickerViewController(allowedUtis, UIDocumentPickerMode.Open);

            documentPicker.AllowsMultipleSelection = allowMultiple;
            documentPicker.Delegate = new PickerDelegate
            {
                PickHandler = urls =>
                {
                    try
                    {
                        // there was a cancellation
                        if (urls?.Any() ?? false)
                        {
                            tcs.TrySetResult(urls.Select(url => new UIDocumentFileResult(url)));
                        }
                        else
                        {
                            tcs.TrySetResult(Enumerable.Empty <FileResult>());
                        }
                    }
                    catch (Exception ex)
                    {
                        // pass exception to task so that it doesn't get lost in the UI main loop
                        tcs.SetException(ex);
                    }
                }
            };

            var parentController = Platform.GetCurrentViewController();

            parentController.PresentViewController(documentPicker, true, null);

            return(tcs.Task);
        }
        static AppTheme PlatformRequestedTheme()
        {
            if (!Platform.HasOSVersion(13, 0))
            {
                return(AppTheme.Unspecified);
            }

            return(Platform.GetCurrentViewController().TraitCollection.UserInterfaceStyle switch
            {
                UIUserInterfaceStyle.Light => AppTheme.Light,
                UIUserInterfaceStyle.Dark => AppTheme.Dark,
                _ => AppTheme.Unspecified
            });
Ejemplo n.º 14
0
        static Task PlatformOpenAsync(OpenFileRequest request)
        {
            var fileUrl = NSUrl.FromFilename(request.File.FullPath);

            var documentController = UIDocumentInteractionController.FromUrl(fileUrl);

            documentController.Uti = request.File.ContentType;

            var vc = Platform.GetCurrentViewController();

            documentController.PresentOpenInMenu(vc.View.Frame, vc.View, true);
            return(Task.CompletedTask);
        }
Ejemplo n.º 15
0
        static async Task <bool> PlatformOpenAsync(Uri uri, BrowserLaunchOptions options)
        {
            switch (options.LaunchMode)
            {
            case BrowserLaunchMode.SystemPreferred:
                var nativeUrl        = new NSUrl(uri.AbsoluteUri);
                var sfViewController = new SFSafariViewController(nativeUrl, false);
                var vc = Platform.GetCurrentViewController();

                if (options.PreferredToolbarColor.HasValue)
                {
                    sfViewController.PreferredBarTintColor = options.PreferredToolbarColor.Value.ToPlatformColor();
                }

                if (options.PreferredControlColor.HasValue)
                {
                    sfViewController.PreferredControlTintColor = options.PreferredControlColor.Value.ToPlatformColor();
                }

                if (sfViewController.PopoverPresentationController != null)
                {
                    sfViewController.PopoverPresentationController.SourceView = vc.View;
                }

                if (options.HasFlag(BrowserLaunchFlags.PresentAsFormSheet))
                {
                    sfViewController.ModalPresentationStyle = UIModalPresentationStyle.FormSheet;
                }
                else if (options.HasFlag(BrowserLaunchFlags.PresentAsPageSheet))
                {
                    sfViewController.ModalPresentationStyle = UIModalPresentationStyle.PageSheet;
                }

                await vc.PresentViewControllerAsync(sfViewController, true);

                break;

            case BrowserLaunchMode.External:
                return(await Launcher.PlatformOpenAsync(uri));
            }

            return(true);
        }
Ejemplo n.º 16
0
        static Task PlatformComposeAsync(EmailMessage message)
        {
            // do this first so we can throw as early as possible
            var parentController = Platform.GetCurrentViewController();

            // create the controller
            var controller = new MFMailComposeViewController();

            if (!string.IsNullOrEmpty(message?.Body))
            {
                controller.SetMessageBody(message.Body, message?.BodyFormat == EmailBodyFormat.Html);
            }
            if (!string.IsNullOrEmpty(message?.Subject))
            {
                controller.SetSubject(message.Subject);
            }
            if (message?.To.Count > 0)
            {
                controller.SetToRecipients(message.To.ToArray());
            }
            if (message?.Cc.Count > 0)
            {
                controller.SetCcRecipients(message.Cc.ToArray());
            }
            if (message?.Bcc.Count > 0)
            {
                controller.SetBccRecipients(message.Bcc.ToArray());
            }

            // show the controller
            var tcs = new TaskCompletionSource <bool>();

            controller.Finished += (sender, e) =>
            {
                controller.DismissViewController(true, null);
                tcs.SetResult(e.Result == MFMailComposeResult.Sent);
            };
            parentController.PresentViewController(controller, true, null);

            return(tcs.Task);
        }
Ejemplo n.º 17
0
        static async Task PlatformRequestAsync(ShareTextRequest request)
        {
            var src   = new TaskCompletionSource <bool>();
            var items = new List <NSObject>();

            if (!string.IsNullOrWhiteSpace(request.Text))
            {
                items.Add(new ShareActivityItemSource(new NSString(request.Text), request.Title));
            }

            if (!string.IsNullOrWhiteSpace(request.Uri))
            {
                items.Add(new ShareActivityItemSource(NSUrl.FromString(request.Uri), request.Title));
            }

            var activityController = new UIActivityViewController(items.ToArray(), null)
            {
                CompletionWithItemsHandler = (a, b, c, d) =>
                {
                    src.TrySetResult(true);
                }
            };

            var vc = Platform.GetCurrentViewController();

            if (activityController.PopoverPresentationController != null)
            {
                activityController.PopoverPresentationController.SourceView = vc.View;

                if (request.PresentationSourceBounds != Rectangle.Empty || Platform.HasOSVersion(13, 0))
                {
                    activityController.PopoverPresentationController.SourceRect = request.PresentationSourceBounds.ToPlatformRectangle();
                }
            }

            await vc.PresentViewControllerAsync(activityController, true);

            await src.Task;
        }
Ejemplo n.º 18
0
        static async Task <IEnumerable <FileResult> > PlatformPickAsync(PickOptions options, bool allowMultiple = false)
        {
            var allowedUtis = options?.FileTypes?.Value?.ToArray() ?? new string[]
            {
                UTType.Content,
                UTType.Item,
                "public.data"
            };

            var tcs = new TaskCompletionSource <IEnumerable <FileResult> >();

            // Use Open instead of Import so that we can attempt to use the original file.
            // If the file is from an external provider, then it will be downloaded.
            using var documentPicker = new UIDocumentPickerViewController(allowedUtis, UIDocumentPickerMode.Open);
            if (Platform.HasOSVersion(11, 0))
            {
                documentPicker.AllowsMultipleSelection = allowMultiple;
            }
            documentPicker.Delegate = new PickerDelegate
            {
                PickHandler = urls => GetFileResults(urls, tcs)
            };

            if (documentPicker.PresentationController != null)
            {
                documentPicker.PresentationController.Delegate = new PickerPresentationControllerDelegate
                {
                    PickHandler = urls => GetFileResults(urls, tcs)
                };
            }

            var parentController = Platform.GetCurrentViewController();

            parentController.PresentViewController(documentPicker, true, null);

            return(await tcs.Task);
        }
Ejemplo n.º 19
0
        static Task PlatformOpenAsync(Uri uri, BrowserLaunchMode launchMode)
        {
            var nativeUrl = new NSUrl(uri.AbsoluteUri);

            switch (launchMode)
            {
            case BrowserLaunchMode.SystemPreferred:
                var sfViewController = new SFSafariViewController(nativeUrl, false);
                var vc = Platform.GetCurrentViewController();

                if (sfViewController.PopoverPresentationController != null)
                {
                    sfViewController.PopoverPresentationController.SourceView = vc.View;
                }
                vc.PresentViewController(sfViewController, true, null);
                break;

            case BrowserLaunchMode.External:
                UIKit.UIApplication.SharedApplication.OpenUrl(nativeUrl);
                break;
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 20
0
        static async Task <FileResult> PhotoAsync(MediaPickerOptions options, bool photo, bool pickExisting)
        {
            var sourceType = pickExisting ? UIImagePickerControllerSourceType.PhotoLibrary : UIImagePickerControllerSourceType.Camera;
            var mediaType  = photo ? UTType.Image : UTType.Movie;

            if (!UIImagePickerController.IsSourceTypeAvailable(sourceType))
            {
                throw new FeatureNotSupportedException();
            }
            if (!UIImagePickerController.AvailableMediaTypes(sourceType).Contains(mediaType))
            {
                throw new FeatureNotSupportedException();
            }

            if (!photo)
            {
                await Permissions.EnsureGrantedAsync <Permissions.Microphone>();
            }

            // permission is not required on iOS 11 for the picker
            if (!Platform.HasOSVersion(11, 0))
            {
                await Permissions.EnsureGrantedAsync <Permissions.Photos>();
            }

            var vc = Platform.GetCurrentViewController(true);

            picker               = new UIImagePickerController();
            picker.SourceType    = sourceType;
            picker.MediaTypes    = new string[] { mediaType };
            picker.AllowsEditing = false;
            if (!photo && !pickExisting)
            {
                picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
            }

            if (!string.IsNullOrWhiteSpace(options?.Title))
            {
                picker.Title = options.Title;
            }

            if (DeviceInfo.Idiom == DeviceIdiom.Tablet && picker.PopoverPresentationController != null && vc.View != null)
            {
                picker.PopoverPresentationController.SourceRect = vc.View.Bounds;
            }

            var tcs = new TaskCompletionSource <FileResult>(picker);

            picker.Delegate = new PhotoPickerDelegate
            {
                CompletedHandler = info =>
                                   tcs.TrySetResult(DictionaryToMediaFile(info))
            };

            await vc.PresentViewControllerAsync(picker, true);

            var result = await tcs.Task;

            await vc.DismissViewControllerAsync(true);

            picker?.Dispose();
            picker = null;

            return(result);
        }
Ejemplo n.º 21
0
        static async Task <FileResult> PhotoAsync(MediaPickerOptions options, bool photo, bool pickExisting)
        {
            var sourceType = pickExisting ? UIImagePickerControllerSourceType.PhotoLibrary : UIImagePickerControllerSourceType.Camera;
            var mediaType  = photo ? UTType.Image : UTType.Movie;

            if (!UIImagePickerController.IsSourceTypeAvailable(sourceType))
            {
                throw new FeatureNotSupportedException();
            }
            if (!UIImagePickerController.AvailableMediaTypes(sourceType).Contains(mediaType))
            {
                throw new FeatureNotSupportedException();
            }

            // microphone only needed if video will be captured
            if (!photo && !pickExisting)
            {
                await Permissions.EnsureGrantedAsync <Permissions.Microphone>();
            }

            // Check if picking existing or not and ensure permission accordingly as they can be set independently from each other
            if (pickExisting && !Platform.HasOSVersion(11, 0))
            {
                await Permissions.EnsureGrantedAsync <Permissions.Photos>();
            }

            if (!pickExisting)
            {
                await Permissions.EnsureGrantedAsync <Permissions.Camera>();
            }

            var vc = Platform.GetCurrentViewController(true);

            picker               = new UIImagePickerController();
            picker.SourceType    = sourceType;
            picker.MediaTypes    = new string[] { mediaType };
            picker.AllowsEditing = false;
            if (!photo && !pickExisting)
            {
                picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
            }

            if (!string.IsNullOrWhiteSpace(options?.Title))
            {
                picker.Title = options.Title;
            }

            if (DeviceInfo.Idiom == DeviceIdiom.Tablet && picker.PopoverPresentationController != null && vc.View != null)
            {
                picker.PopoverPresentationController.SourceRect = vc.View.Bounds;
            }

            var tcs = new TaskCompletionSource <FileResult>(picker);

            picker.Delegate = new PhotoPickerDelegate
            {
                CompletedHandler = async info =>
                {
                    GetFileResult(info, tcs);
                    await vc.DismissViewControllerAsync(true);
                }
            };

            if (picker.PresentationController != null)
            {
                picker.PresentationController.Delegate = new PhotoPickerPresentationControllerDelegate
                {
                    CompletedHandler = info => GetFileResult(info, tcs)
                };
            }

            await vc.PresentViewControllerAsync(picker, true);

            var result = await tcs.Task;

            picker?.Dispose();
            picker = null;

            return(result);
        }