Exemplo n.º 1
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.Zero || Platform.HasOSVersion(13, 0))
                {
                    activityController.PopoverPresentationController.SourceRect = request.PresentationSourceBounds.AsCGRect();
                }
            }

            return(vc.PresentViewControllerAsync(activityController, true));
        }
Exemplo n.º 2
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?.TrySetResult(e.Result == MessageComposeResult.Sent);
            };
            controller.PresentViewController(messageController, true, null);

            return(tcs.Task);
        }
Exemplo n.º 3
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 (request.PresentationSourceBounds != Rectangle.Zero || Platform.HasOSVersion(13, 0))
                {
                    activityController.PopoverPresentationController.SourceRect = request.PresentationSourceBounds.AsCGRect();
                }
            }

            return(vc.PresentViewControllerAsync(activityController, true));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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 Platform.UIPresentationControllerDelegate(() => GetFileResults(null, tcs));
            }

            var parentController = Platform.GetCurrentViewController();

            parentController.PresentViewController(documentPicker, true, null);

            return(await tcs.Task);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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 != null)
                {
                    sfViewController.PreferredBarTintColor = options.PreferredToolbarColor.AsUIColor();
                }

                if (options.PreferredControlColor != null)
                {
                    sfViewController.PreferredControlTintColor = options.PreferredControlColor.AsUIColor();
                }

                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);
        }
Exemplo n.º 8
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>();
            }

            // 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 = info => GetFileResult(info, tcs)
            };

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

            await vc.PresentViewControllerAsync(picker, true);

            var result = await tcs.Task;

            await vc.DismissViewControllerAsync(true);

            picker?.Dispose();
            picker = null;

            return(result);
        }