How To Pick A Document In Xamarin.iOS

How To Pick A Document In Xamarin.iOS
 

Introduction

 
This blog demonstrates how to pick a file in an iOS device in a Xamarin iOS application. By default it must enable the iCloud provisioning certificate, however, I need to pick from the local device, not in the iOS Cloud, so provisioning a profile is not necessary. 
 
How To Pick A Document In Xamarin.iOS
 
Prerequisites
  • Visual Studio for Mac
  • XCode
  • Simulator 
Let's Start ❤
 
Step 1
 
Open Visual Studio for Mac >> New >> In the left plane select App under iOS >> center plane select Single View App >> click Next.
 
How To Pick A Document In Xamarin.iOS
 
Step 2
 
Next, give your Application Name, Device type, Identifier, select Target Version and click OK.
 
How To Pick A Document In Xamarin.iOS
 
Next check the app name and solution name, then click Create.
 
Step 3
 
Now, open Storyboard in your interface builder using the right click of Main.Storyboard >> in the context menu select Open with >> followed by XCode Interface Builder (because my favorite designer is XCode). Afterward, drag the button from the Toolbox and place it in the center of the scene. Next  >> Solution Explorer >> open Main.Storyboard >> drag to place the button in the center of the scene and set perfect constraints and create a click event for this button:
How To Pick A Document In Xamarin.iOS 
Now, open ViewController.cs file and write the code given below, by going to Solution Explorer >> double click and open ViewController.cs file. In the code, I created the Document View Controller and presented it in the popview inside the event we created before. In case the controller has been canceled the WasCancelled event will be invoked or DidPickDocumentAturls delegate will be invoked.
 
How To Pick A Document In Xamarin.iOS 
  1. partial void BtnDirectPick_TouchUpInside(UIButton sender)  
  2. {  
  3.     var picker = new UIDocumentPickerViewController(allowedUTIs, UIDocumentPickerMode.Open);  
  4.     picker.WasCancelled += Picker_WasCancelled;  
  5.     picker.DidPickDocumentAtUrls += (object s, UIDocumentPickedAtUrlsEventArgs e) =>  
  6.     {  
  7.         Console.WriteLine("url = {0}", e.Urls[0].AbsoluteString);  
  8.         //bool success = await MoveFileToApp(didPickDocArgs.Url);  
  9.         var success = true;  
  10.         string filename = e.Urls[0].LastPathComponent;  
  11.         string msg = success ? string.Format("Successfully imported file '{0}'", filename) : string.Format("Failed to import file '{0}'", filename);  
  12.        // Some invaild file url returns null  
  13.         NSData data = NSData.FromUrl(e.Urls[0]);  
  14.        if(data!=null){  
  15.         byte[] dataBytes = new byte[data.Length];  
  16.   
  17.         System.Runtime.InteropServices.Marshal.Copy(data.Bytes, dataBytes, 0, Convert.ToInt32(data.Length));  
  18.   
  19.         for (int i = 0; i < dataBytes.Length; i++)  
  20.         {  
  21.             Console.WriteLine(dataBytes[i]);  
  22.         }  
  23.         }  
  24.   
  25.         Console.WriteLine(data + "Completed");  
  26.   
  27.         var alertController = UIAlertController.Create("import", msg, UIAlertControllerStyle.Alert);  
  28.         var okButton = UIAlertAction.Create("OK", UIAlertActionStyle.Default, (obj) =>  
  29.         {  
  30.             alertController.DismissViewController(truenull);  
  31.         });  
  32.         alertController.AddAction(okButton);  
  33.         PresentViewController(alertController, truenull);  
  34.     };  
  35.     PresentViewController(picker, truenull);  
  36. }  
WasCancelled Method:
  1. private void Picker_WasCancelled(object sender, EventArgs e)  
  2.  {  
  3.      Console.WriteLine("Picker was Cancelled");  
  4.  }  
If you need to filter a specific format file, use the below URI filters:
 
How To Pick A Document In Xamarin.iOS
  1. private string[] allowedUTIs =  {  
  2.                     UTType.UTF8PlainText,  
  3.                     UTType.PlainText,  
  4.                     UTType.RTF,  
  5.                     UTType.PNG,  
  6.                     UTType.Text,  
  7.                     UTType.PDF,  
  8.                     UTType.Image  
  9.                 };  
Step 4
 
Press F5 or run the project, you will get output like below.
 
How To Pick A Document In Xamarin.iOS
 

Summary

  • Create UIDocumentPickerViewController
  • Implement picker delegate
  • Convert NSData to byte.
In this article, we learned how to use the document view controller in Xamarin iOS.