Ejemplo n.º 1
0
 void DocPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
 {
     e.Url.StartAccessingSecurityScopedResource();
     DocName.Text = e.Url.LastPathComponent;
     DocPath.Text = e.Url.AbsoluteString;
     e.Url.StopAccessingSecurityScopedResource();
 }
        private void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
        {
            try
            {
                var securityEnabled = e.Url.StartAccessingSecurityScopedResource();
                var doc             = new UIDocument(e.Url);
                var data            = NSData.FromUrl(e.Url);
                var dataBytes       = new byte [data.Length];

                System.Runtime.InteropServices.Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length));

                string filename = doc.LocalizedName;
                string pathname = doc.FileUrl?.Path;

                e.Url.StopAccessingSecurityScopedResource();

                // iCloud drive can return null for LocalizedName.
                if (filename == null && pathname != null)
                {
                    filename = Path.GetFileName(pathname);
                }

                OnFilePicked(new FilePickerEventArgs(dataBytes, filename, pathname));
            }
            catch (Exception ex)
            {
                // pass exception to task so that it doesn't get lost in the UI main loop
                var tcs = Interlocked.Exchange(ref _completionSource, null);
                tcs.SetException(ex);
            }
        }
Ejemplo n.º 3
0
        private void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
        {
            var securityEnabled = e.Url.StartAccessingSecurityScopedResource();
            var doc             = new UIDocument(e.Url);
            var data            = NSData.FromUrl(e.Url);
            var dataBytes       = new byte[data.Length];

            System.Runtime.InteropServices.Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length));

            var filename = doc.LocalizedName;

            // 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

                // pathname is either a string or null.
                var pathname = doc.FileUrl?.ToString();

                // 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);
            }

            OnFilePicked(new FilePickerEventArgs(filename));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Callback method called by document picker when file has been picked; this is called
        /// up to iOS 10.
        /// </summary>
        /// <param name="sender">sender object (document picker)</param>
        /// <param name="args">event args</param>
        void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs args)
        {
            try
            {
                var securityEnabled = args.Url.StartAccessingSecurityScopedResource();
                var doc             = new UIDocument(args.Url);

                string name = doc.LocalizedName;
                string path = doc.FileUrl?.Path;

                args.Url.StopAccessingSecurityScopedResource();

                // iCloud drive can return null for LocalizedName.
                if (name == null && path != null)
                {
                    name = Path.GetFileName(path);
                }

                NSFileManager   mg = new NSFileManager();
                List <FileData> ls = new List <FileData>();
                ls.Add(new FileData(path, name, mg.GetAttributes(path).Size.Value));
                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);
            }
        }
        /// <summary>
        /// Callback method called by document picker when file has been picked; this is called
        /// up to iOS 10.
        /// </summary>
        /// <param name="sender">sender object (document picker)</param>
        /// <param name="args">event args</param>
        private void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs args)
        {
            try
            {
                var securityEnabled = args.Url.StartAccessingSecurityScopedResource();
                var doc             = new UIDocument(args.Url);
                var filename        = doc.LocalizedName;
                var pathname        = doc.FileUrl?.Path;
                args.Url.StopAccessingSecurityScopedResource();

                // iCloud drive can return null for LocalizedName.
                if (filename == null && pathname != null)
                {
                    filename = Path.GetFileName(pathname);
                }

                var tcs = Interlocked.Exchange(ref completionSource, null);
                tcs?.TrySetResult(new FileData(pathname, filename, () => new FileStream(pathname, FileMode.Open, FileAccess.Read)));
            }
            catch (Exception ex)
            {
                // pass exception to task so that it doesn't get lost in the UI main loop
                var tcs = Interlocked.Exchange(ref completionSource, null);
                tcs?.TrySetException(ex);
            }
            finally
            {
                var control = (UIDocumentPickerViewController)sender;
                control.Dispose();
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Callback method called by document picker when file has been picked; this is called
 /// up to iOS 10.
 /// </summary>
 /// <param name="sender">sender object (document picker)</param>
 /// <param name="args">event args</param>
 private static void Pvc_DidPickDocument(object sender, UIDocumentPickedEventArgs args)
 {
     try
     {
         /*
          * var securityEnabled = args.Url.StartAccessingSecurityScopedResource();
          * var doc = new UIDocument(args.Url);
          *
          * string filename = doc.LocalizedName;
          * string pathname = doc.FileUrl?.Path;
          *
          * args.Url.StopAccessingSecurityScopedResource();
          * if (!string.IsNullOrWhiteSpace(pathname))
          * {
          *  // iCloud drive can return null for LocalizedName.
          *  if (filename == null && pathname != null)
          *      filename = System.IO.Path.GetFileName(pathname);
          *
          *  tcs.TrySetResult(new StorageFile(pathname));
          * }
          * else
          *  tcs.TrySetResult(null);
          */
         tcs.TrySetResult(new StorageFile(args.Url));
     }
     catch (Exception ex)
     {
         tcs.SetException(ex);
     }
     tcs = null;
     pvc.Dispose();
     pvc = null;
 }
        /// <summary>
        /// Callback method called by document picker when file has been picked; this is called
        /// up to iOS 10.
        /// </summary>
        /// <param name="sender">sender object (document picker)</param>
        /// <param name="args">event args</param>
        private void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs args)
        {
            try
            {
                var securityEnabled = args.Url.StartAccessingSecurityScopedResource();
                var doc             = new UIDocument(args.Url);

                string filename = doc.LocalizedName;
                string pathname = doc.FileUrl?.Path;

                args.Url.StopAccessingSecurityScopedResource();

                // iCloud drive can return null for LocalizedName.
                if (filename == null && pathname != null)
                {
                    filename = Path.GetFileName(pathname);
                }

                this.OnFilePicked(new FilePickerEventArgs(filename, pathname));
            }
            catch (Exception ex)
            {
                // pass exception to task so that it doesn't get lost in the UI main loop
                var tcs = Interlocked.Exchange(ref this.completionSource, null);
                tcs.SetException(ex);
            }
        }
Ejemplo n.º 8
0
        private void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
        {
            e.Url.StartAccessingSecurityScopedResource();

            var doc      = new UIDocument(e.Url);
            var fileName = doc.LocalizedName;

            if (string.IsNullOrWhiteSpace(fileName))
            {
                var path = doc.FileUrl?.ToString();
                if (path != null)
                {
                    path = WebUtility.UrlDecode(path);
                    var split = path.LastIndexOf('/');
                    fileName = path.Substring(split + 1);
                }
            }

            var     fileCoordinator = new NSFileCoordinator();
            NSError error;

            fileCoordinator.CoordinateRead(e.Url, NSFileCoordinatorReadingOptions.WithoutChanges, out error, (url) =>
            {
                var data = NSData.FromUrl(url).ToArray();
                SelectFileResult(data, fileName ?? "unknown_file_name");
            });

            e.Url.StopAccessingSecurityScopedResource();
        }
Ejemplo n.º 9
0
        void DidPickDocumentForExport(object sender, UIDocumentPickedEventArgs e)
        {
            // The URL refers to the new copy of the exported document at the selected destination.
            // This URL refers to a file outside your app’s sandbox.
            // You cannot access this copy; the URL is passed only to indicate success.
            NSUrl url = e.Url;

            Console.WriteLine("{0} exported to new location outside your app’s sandbox {1}", documentURL, url);
        }
Ejemplo n.º 10
0
        void DidPickDocumentForMove(object sender, UIDocumentPickedEventArgs e)
        {
            // The URL refers to the document’s new location.
            // The provided URL is a security-scoped URL referring to a file outside your app’s sandbox.
            // For more information on working with external, security-scoped URLs, see Requirements in Document Picker Programming Guide.
            // https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentPickerProgrammingGuide/AccessingDocuments/AccessingDocuments.html#//apple_ref/doc/uid/TP40014451-CH2-SW3
            NSUrl securityScopedUrl = e.Url;

            PrintOutsideFileContent(securityScopedUrl);
        }
Ejemplo n.º 11
0
        void DidPickDocumentForImport(object sender, UIDocumentPickedEventArgs e)
        {
            // The url refers to a copy of the selected document.
            // This document is a temporary file.
            // It remains available only until your application terminates.
            // To keep a permanent copy, you must move this file to a permanent location inside your sandbox.
            NSUrl temporaryFileUrl = e.Url;

            PrintFileContent(temporaryFileUrl);
        }
        void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
        {
            var securityEnabled = e.Url.StartAccessingSecurityScopedResource();

            var doc = new UIDocument(e.Url);

            var data = NSData.FromUrl(e.Url);

            byte[] dataBytes = new byte[data.Length];

            System.Runtime.InteropServices.Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length));

            OnFilePicked(new FilePickerEventArgs(dataBytes, doc.LocalizedName));
        }
Ejemplo n.º 13
0
        void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
        {
            var securityEnabled = e.Url.StartAccessingSecurityScopedResource();
            var doc             = new UIDocument(e.Url);
            var data            = NSData.FromUrl(e.Url);

            byte[] dataBytes = new byte[data.Length];
            System.Runtime.InteropServices.Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length));
            string filename = doc.LocalizedName;
            var    pathname = doc.FileUrl?.ToString();

            if (filename == null)
            {
                var filesplit = pathname?.LastIndexOf('/') ?? 0;
                filename = pathname?.Substring(filesplit + 1);
                string normalizedFilename = filename.Replace("%20", " ");
                OnFilePicked(new FilePickerEventArgs(dataBytes, normalizedFilename));
            }
            else
            {
                OnFilePicked(new FilePickerEventArgs(dataBytes, filename));
            }
        }
        void OnDocumentPicked(object sender, UIDocumentPickedEventArgs e)
        {
            Console.WriteLine("OnDocumentPicked called");
            bool startAccessingWorked = e.Url.StartAccessingSecurityScopedResource();

            NSUrl ubiquityURL = NSFileManager.DefaultManager.GetUrlForUbiquityContainer(null);

            Console.WriteLine("ubiquityURL {0}", ubiquityURL);
            Console.WriteLine("start {0}", startAccessingWorked);

            // TODO: This should work but doesn't
//			NSFileCoordinator fileCoordinator = new NSFileCoordinator ();
//			NSError error = null;
//			Console.WriteLine ("MainViewController before CoordinateRead {0}", e.Url);
//			fileCoordinator.CoordinateRead (e.Url, (NSFileCoordinatorReadingOptions)0, out error, newUrl => {
//				Console.WriteLine ("MainViewController inside CoordinateRead");
//				NSData data = NSData.FromUrl(newUrl);
//				Console.WriteLine ("error {0}", error);
//				Console.WriteLine ("data {0}", data);
//			});
//			Console.WriteLine ("MainViewController after CoordinateRead error {0}", error);

            e.Url.StopAccessingSecurityScopedResource();
        }
Ejemplo n.º 15
0
 private void Pvc_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
 {
 }
Ejemplo n.º 16
0
		void DidPickDocumentForImport (object sender, UIDocumentPickedEventArgs e)
		{
			// The url refers to a copy of the selected document.
			// This document is a temporary file.
			// It remains available only until your application terminates.
			// To keep a permanent copy, you must move this file to a permanent location inside your sandbox.
			NSUrl temporaryFileUrl = e.Url;
			PrintFileContent (temporaryFileUrl);
		}
Ejemplo n.º 17
0
 private void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
 {
     PickedDocument(e.Url);
 }
Ejemplo n.º 18
0
		void DidPickDocumentForExport (object sender, UIDocumentPickedEventArgs e)
		{
			// The URL refers to the new copy of the exported document at the selected destination.
			// This URL refers to a file outside your app’s sandbox.
			// You cannot access this copy; the URL is passed only to indicate success.
			NSUrl url = e.Url;
			Console.WriteLine ("{0} exported to new location outside your app’s sandbox {1}", documentURL, url);
		}
Ejemplo n.º 19
0
		void DidPickDocumentForMove (object sender, UIDocumentPickedEventArgs e)
		{
			// The URL refers to the document’s new location.
			// The provided URL is a security-scoped URL referring to a file outside your app’s sandbox.
			// For more information on working with external, security-scoped URLs, see Requirements in Document Picker Programming Guide.
			// https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentPickerProgrammingGuide/AccessingDocuments/AccessingDocuments.html#//apple_ref/doc/uid/TP40014451-CH2-SW3
			NSUrl securityScopedUrl = e.Url;
			PrintOutsideFileContent (securityScopedUrl);
		}
		void OnDocumentPicked (object sender, UIDocumentPickedEventArgs e)
		{
			Console.WriteLine ("OnDocumentPicked called");
			bool startAccessingWorked = e.Url.StartAccessingSecurityScopedResource ();

			NSUrl ubiquityURL = NSFileManager.DefaultManager.GetUrlForUbiquityContainer (null);
			Console.WriteLine ("ubiquityURL {0}", ubiquityURL);
			Console.WriteLine ("start {0}", startAccessingWorked);

			// TODO: This should work but doesn't
//			NSFileCoordinator fileCoordinator = new NSFileCoordinator ();
//			NSError error = null;
//			Console.WriteLine ("MainViewController before CoordinateRead {0}", e.Url);
//			fileCoordinator.CoordinateRead (e.Url, (NSFileCoordinatorReadingOptions)0, out error, newUrl => {
//				Console.WriteLine ("MainViewController inside CoordinateRead");
//				NSData data = NSData.FromUrl(newUrl);
//				Console.WriteLine ("error {0}", error);
//				Console.WriteLine ("data {0}", data);
//			});
//			Console.WriteLine ("MainViewController after CoordinateRead error {0}", error);

			e.Url.StopAccessingSecurityScopedResource ();
		}