/// <summary> /// Callback method called by document picker when file has been picked; this is called /// starting from iOS 11. /// </summary> /// <param name="sender">sender object (document picker)</param> /// <param name="args">event args</param> void DocumentPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs args) { try { List <FileData> ls = new List <FileData>(); NSFileManager mg = new NSFileManager(); foreach (var url in args.Urls) { url.StartAccessingSecurityScopedResource(); var doc = new UIDocument(url); string name = doc.LocalizedName; string path = doc.FileUrl?.Path; // iCloud drive can return null for LocalizedName. if (name == null && path != null) { name = Path.GetFileName(path); } ls.Add(new FileData(path, name, mg.GetAttributes(path).Size.Value)); url.StopAccessingSecurityScopedResource(); } var tcs = Interlocked.Exchange(ref _tcs, null); tcs?.SetResult(ls); } catch (Exception ex) { // pass exception to task so that it doesn't get lost in the UI main loop var tcs = Interlocked.Exchange(ref _tcs, null); tcs?.SetException(ex); } }
private void DocumentPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e) { var control = (UIDocumentPickerViewController)sender; foreach (var url in e.Urls) { DocumentPicker_DidPickDocument(control, new UIDocumentPickedEventArgs(url)); } control.Dispose(); }
/// <summary> /// Callback method called by document picker when file has been picked; this is called /// starting from iOS 11. /// /// Note that this class supports only the picking of 1 file /// </summary> /// <param name="sender">sender object (document picker)</param> /// <param name="args">event args</param> private void DocumentPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs args) { if (args.Urls.Length == 0) { DocumentPicker_WasCancelled(sender, EventArgs.Empty); } else { DocumentPicker_DidPickDocument(sender, new UIDocumentPickedEventArgs(args.Urls[0])); } }
private void Pvc_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e) { try { var path = e.Urls.First().Path; _pickerCallback(path); } catch (Exception ex) { } }
/// <summary> /// Callback method called by document picker when file has been picked; this is called /// starting from iOS 11. /// </summary> /// <param name="sender">sender object (document picker)</param> /// <param name="args">event args</param> private static void Pvc_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs args) { var control = (UIDocumentPickerViewController)sender; var count = args.Urls.Count(); if (count == 0) { Pvc_WasCancelled(sender, null); } else { Pvc_DidPickDocument(control, new UIDocumentPickedEventArgs(args.Urls[0])); } control.Dispose(); }
private void FinishedSelectingImageFromFiles(object sender, UIDocumentPickedAtUrlsEventArgs e) { try { NSUrl url = e.Urls[0]; NSData imageData = NSData.FromUrl(url); UIImage image = new UIImage(imageData); LaunchCropViewController(image, url, this.NavigationController); } catch (Exception exception) { var alert = UIAlertController.Create("Invalid Selection", "Please select an image instead.", UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); } }
void DocPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e) { if (e.Urls == null || e.Urls.Length < 1) { return; } docPicker?.DismissViewControllerAsync(true); // IMPORTANT! You must lock the security scope before you can // access this file var securityEnabled = e.Urls[0].StartAccessingSecurityScopedResource(); ThisApp.ClearDocumentHandler(); ThisApp.DocumentLoaded += ThisApp_DocumentLoaded; ThisApp.OpenDocument(e.Urls[0]); // IMPORTANT! You must release the security lock established // above. e.Urls[0].StopAccessingSecurityScopedResource(); }
private void DocumentPicker_DidPickDocumentAtUrls(object sender, UIDocumentPickedAtUrlsEventArgs e) { _kind = OldOrNew.New; var securityEnabled = e.Urls.FirstOrDefault().StartAccessingSecurityScopedResource(); var doc = new UIDocument(e.Urls[0]); string filename = doc.LocalizedName; string pathname = doc.FileUrl?.ToString(); // iCloud drive can return null for LocalizedName. if (filename == null) { // Retrieve actual filename by taking the last entry after / in FileURL. // e.g. /path/to/file.ext -> file.ext // filesplit is either: // 0 (pathname is null, or last / is at position 0) // -1 (no / in pathname) // positive int (last occurence of / in string) var filesplit = pathname?.LastIndexOf('/') ?? 0; filename = pathname?.Substring(filesplit + 1); } var refurl = NSData.FromUrl(e.Urls[0].FileReferenceUrl); var refurldataBytes = new byte[refurl.Length]; System.Runtime.InteropServices.Marshal.Copy(refurl.Bytes, refurldataBytes, 0, Convert.ToInt32(refurl.Length)); //var Pathurl = NSData.FromUrl(e.Urls[0].FilePathUrl); //var PathurldataBytes = new byte[Pathurl.Length]; //System.Runtime.InteropServices.Marshal.Copy(Pathurl.Bytes, PathurldataBytes, 0, Convert.ToInt32(Pathurl.Length)); var data = NSData.FromUrl(e.Urls[0]);// NSUrl.FromString(pathname)); var dataBytes = new byte[data.Length]; System.Runtime.InteropServices.Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length)); OnFilePicked(new FilePickerEventArgs(dataBytes, filename, pathname)); }