Пример #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));
        }
Пример #2
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);
        }
Пример #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));
        }
Пример #4
0
 static void PlatformLongPress()
 {
     if (Platform.HasOSVersion(10, 0))
     {
         var impact = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Medium);
         impact.Prepare();
         impact.ImpactOccurred();
         impact.Dispose();
     }
 }
Пример #5
0
 static void PlatformClick()
 {
     if (Platform.HasOSVersion(10, 0))
     {
         var impact = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Light);
         impact.Prepare();
         impact.ImpactOccurred();
         impact.Dispose();
     }
 }
        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
            });
Пример #7
0
        internal static CLAuthorizationStatus GetAuthorizationStatus(this CLLocationManager locationManager)
        {
#if __MACOS__
            if (DeviceInfo.Version >= new Version(11, 0))
#elif __WATCHOS__
            if (Platform.HasOSVersion(7, 0))
#else
            if (Platform.HasOSVersion(14, 0))
#endif
            {
                // return locationManager.AuthorizationStatus;

                var sel = ObjCRuntime.Selector.GetHandle("authorizationStatus");
                return(CLAuthorizationStatus_objc_msgSend(locationManager.Handle, sel));
            }

            return(CLLocationManager.Status);
        }
Пример #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);
        }
Пример #9
0
        static FileResult DictionaryToMediaFile(NSDictionary info)
        {
            if (info == null)
            {
                return(null);
            }

            PHAsset phAsset  = null;
            NSUrl   assetUrl = null;

            if (Platform.HasOSVersion(11, 0))
            {
                assetUrl = info[UIImagePickerController.ImageUrl] as NSUrl;

                // Try the MediaURL sometimes used for videos
                if (assetUrl == null)
                {
                    assetUrl = info[UIImagePickerController.MediaURL] as NSUrl;
                }

                if (assetUrl != null)
                {
                    if (!assetUrl.Scheme.Equals("assets-library", StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(new UIDocumentFileResult(assetUrl));
                    }

                    phAsset = info.ValueForKey(UIImagePickerController.PHAsset) as PHAsset;
                }
            }

            if (phAsset == null)
            {
                assetUrl = info[UIImagePickerController.ReferenceUrl] as NSUrl;

                if (assetUrl != null)
                {
                    phAsset = PHAsset.FetchAssets(new NSUrl[] { assetUrl }, null)?.LastObject as PHAsset;
                }
            }

            if (phAsset == null || assetUrl == null)
            {
                var img = info.ValueForKey(UIImagePickerController.OriginalImage) as UIImage;

                if (img != null)
                {
                    return(new UIImageFileResult(img));
                }
            }

            if (phAsset == null || assetUrl == null)
            {
                return(null);
            }

            string originalFilename;

            if (Platform.HasOSVersion(9, 0))
            {
                originalFilename = PHAssetResource.GetAssetResources(phAsset).FirstOrDefault()?.OriginalFilename;
            }
            else
            {
                originalFilename = phAsset.ValueForKey(new NSString("filename")) as NSString;
            }

            return(new PHAssetFileResult(assetUrl, phAsset, originalFilename));
        }
Пример #10
0
 internal static Task <bool> PlatformOpenAsync(NSUrl nativeUrl) =>
 Platform.HasOSVersion(10, 0)
                         ? UIApplication.SharedApplication.OpenUrlAsync(nativeUrl, new UIApplicationOpenUrlOptions())
                         : Task.FromResult(UIApplication.SharedApplication.OpenUrl(nativeUrl));