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

            var uiStyle = Platform.GetCurrentUIViewController()?.TraitCollection?.UserInterfaceStyle ??
                          UITraitCollection.CurrentTraitCollection.UserInterfaceStyle;

            return(uiStyle switch
            {
                UIUserInterfaceStyle.Light => AppTheme.Light,
                UIUserInterfaceStyle.Dark => AppTheme.Dark,
                _ => AppTheme.Unspecified
            });
예제 #2
0
        static Task PlatformOpenAsync(OpenFileRequest request)
        {
            documentController = new UIDocumentInteractionController()
            {
                Name = request.File.FileName,
                Url  = NSUrl.FromFilename(request.File.FullPath),
                Uti  = request.File.ContentType
            };

            var view = Platform.GetCurrentUIViewController().View;
            var rect = DeviceInfo.Idiom == DeviceIdiom.Tablet
                ? new CGRect(new CGPoint(view.Bounds.Width / 2, view.Bounds.Height), CGRect.Empty.Size)
                : view.Bounds;

            documentController.PresentOpenInMenu(rect, view, true);
            return(Task.CompletedTask);
        }
        internal static async Task <WebAuthenticatorResult> PlatformAuthenticateAsync(Uri url, Uri callbackUrl)
        {
            // Cancel any previous task that's still pending
            if (tcsResponse?.Task != null && !tcsResponse.Task.IsCompleted)
            {
                tcsResponse.TrySetCanceled();
            }

            tcsResponse = new TaskCompletionSource <WebAuthenticatorResult>();
            redirectUri = callbackUrl;

            try
            {
                var scheme = redirectUri.Scheme;

                if (!VerifyHasUrlSchemeOrDoesntRequire(scheme))
                {
                    tcsResponse.TrySetException(new InvalidOperationException("You must register your URL Scheme handler in your app's Info.plist!"));
                    return(await tcsResponse.Task);
                }

#if __IOS__
                void AuthSessionCallback(NSUrl cbUrl, NSError error)
                {
                    if (error == null)
                    {
                        OpenUrl(cbUrl);
                    }
                    else
                    {
                        tcsResponse.TrySetException(new NSErrorException(error));
                    }
                }

                if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
                {
                    was = new ASWebAuthenticationSession(new NSUrl(url.OriginalString), scheme, AuthSessionCallback);

                    if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
                    {
                        var ctx = new ContextProvider(Platform.GetCurrentWindow());
                        void_objc_msgSend_IntPtr(was.Handle, ObjCRuntime.Selector.GetHandle("setPresentationContextProvider:"), ctx.Handle);
                    }

                    using (was)
                    {
                        was.Start();
                        return(await tcsResponse.Task);
                    }
                }

                if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
                {
                    sf = new SFAuthenticationSession(new NSUrl(url.OriginalString), scheme, AuthSessionCallback);
                    using (sf)
                    {
                        sf.Start();
                        return(await tcsResponse.Task);
                    }
                }

                // THis is only on iOS9+ but we only support 10+ in Essentials anyway
                var controller = new SFSafariViewController(new NSUrl(url.OriginalString), false)
                {
                    Delegate = new NativeSFSafariViewControllerDelegate
                    {
                        DidFinishHandler = (svc) =>
                        {
                            // Cancel our task if it wasn't already marked as completed
                            if (!(tcsResponse?.Task?.IsCompleted ?? true))
                            {
                                tcsResponse.TrySetException(new OperationCanceledException());
                            }
                        }
                    },
                };

                currentViewController = controller;
                await Platform.GetCurrentUIViewController().PresentViewControllerAsync(controller, true);

                return(await tcsResponse.Task);
#else
                var opened = UIApplication.SharedApplication.OpenUrl(url);
                if (!opened)
                {
                    tcsResponse.TrySetException(new Exception("Error opening Safari"));
                }
#endif
            }
            catch (Exception ex)
            {
                tcsResponse.TrySetException(ex);
            }

            return(await tcsResponse.Task);
        }