How To Pick Document In Xamarin.iOS
Published: 2020-02-02 09:06:18

---
title: How To Pick Document In Xamarin.iOS
published: true
date: 2020-01-31 18:24:00 UTC
tags: 
canonical_url: https://logeshpalani.blogspot.com/2020/01/how-to-pick-document-in-xamarinios.html
---

[![](https://1.bp.blogspot.com/-b4UAcWRIwfA/XjMaoGOU7NI/AAAAAAAAPzY/kl2gLxlg-TwzCPKWTi6eLax89LJVeZjAACPcBGAYYCw/s1600/How%2Bto%2Bpick%2Bdocument%2Bin%2BXamarin%2BiOS.png)](https://1.bp.blogspot.com/-b4UAcWRIwfA/XjMaoGOU7NI/AAAAAAAAPzY/kl2gLxlg-TwzCPKWTi6eLax89LJVeZjAACPcBGAYYCw/s1600/How%2Bto%2Bpick%2Bdocument%2Bin%2BXamarin%2BiOS.png)

## Introduction 

This blog demonstrates, how to pick a file in the iOS device in the Xamarin iOS application. In-default must enable the iCloud provisioning certificate, however, I need pick from the local device, not in iOS Cloud, so provisioning profile is not necessary.

  

[![](https://1.bp.blogspot.com/-UogobdZRUT0/XjMY8e1AGaI/AAAAAAAAPzM/hMcn9yOzyfMij3D15xja6SqMCipivzSJwCPcBGAYYCw/s320/IMG_0708.jpg)](https://1.bp.blogspot.com/-UogobdZRUT0/XjMY8e1AGaI/AAAAAAAAPzM/hMcn9yOzyfMij3D15xja6SqMCipivzSJwCPcBGAYYCw/s1600/IMG_0708.jpg)
  

## **Prerequisites**

- Visual Studio for Mac
- XCode (optional)
- Simulator or Physical Device

### Step 1:

Open _Visual Studio for Mac_ >> New >> In the left plane select App under _iOS_ >>

center plane select _Single View App_ >> click _Next_.  
  

[![](https://1.bp.blogspot.com/-MdNNQXfxjTg/XjMY8YcrvhI/AAAAAAAAPzA/oCzwq8Z7RS01yAlOzOpxFbM244YNCEZSwCPcBGAYYCw/s320/Screenshot%2B2020-01-27%2Bat%2B6.19.20%2BPM.png)](https://1.bp.blogspot.com/-MdNNQXfxjTg/XjMY8YcrvhI/AAAAAAAAPzA/oCzwq8Z7RS01yAlOzOpxFbM244YNCEZSwCPcBGAYYCw/s1600/Screenshot%2B2020-01-27%2Bat%2B6.19.20%2BPM.png)
  

### Step 2:

Next, give your _Application Name_, _Device type, Identifier_, select _Target Version_ and click _Ok_.

  

[![](https://1.bp.blogspot.com/-dI1Uy9t5ohY/XjMY9Y4hR2I/AAAAAAAAPzE/okg58cHQJVcxdTTl2odZMk-GOd4SSQEMACPcBGAYYCw/s320/Screenshot%2B2020-01-27%2Bat%2B6.19.55%2BPM.png)](https://1.bp.blogspot.com/-dI1Uy9t5ohY/XjMY9Y4hR2I/AAAAAAAAPzE/okg58cHQJVcxdTTl2odZMk-GOd4SSQEMACPcBGAYYCw/s1600/Screenshot%2B2020-01-27%2Bat%2B6.19.55%2BPM.png)
  
  

followed by check the app name and solution name, then click Create.  
  

[![](https://1.bp.blogspot.com/-LZcJcLYY-rA/Xisvq6P0_dI/AAAAAAAAPw8/BXnqkanhsH4wiNpyu5buRSrT1CpGnWz7wCPcBGAYYCw/s320/Screenshot%2B2020-01-23%2Bat%2B7.55.29%2BPM.png)](https://1.bp.blogspot.com/-LZcJcLYY-rA/Xisvq6P0_dI/AAAAAAAAPw8/BXnqkanhsH4wiNpyu5buRSrT1CpGnWz7wCPcBGAYYCw/s1600/Screenshot%2B2020-01-23%2Bat%2B7.55.29%2BPM.png)
  

### 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 of my favorite designer is XCode). Afterward, drag Button from Toolbox and place in the center of the scene. by doing >> _Solution Explorer_ >> open _Main.Storyboard_ >> drag to place Button in the center of the scene and set perfect constraints and Create a click event for this button.

  

[![](https://1.bp.blogspot.com/-8scqkX26-S4/XjMY8SHoMCI/AAAAAAAAPzM/vMHfBmoVDPYkszih5EC7euFeMWm4Z17SwCPcBGAYYCw/s1600/Button.png)](https://1.bp.blogspot.com/-8scqkX26-S4/XjMY8SHoMCI/AAAAAAAAPzM/vMHfBmoVDPYkszih5EC7euFeMWm4Z17SwCPcBGAYYCw/s1600/Button.png)
  
  

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 Present in the popview inside the event, we created before. In case the controller was canceled the WasCancelled event will be invoked or DidPickDocumentAturls delegate will be invoked.  
  

[![](https://1.bp.blogspot.com/-WlGFihU1wiY/XjMY9syDuFI/AAAAAAAAPzM/ejY7I6N5_K8tonMx3gaPXQcjoWSLMwzJgCPcBGAYYCw/s320/Screenshot%2B2020-01-29%2Bat%2B8.18.40%2BPM.png)](https://1.bp.blogspot.com/-WlGFihU1wiY/XjMY9syDuFI/AAAAAAAAPzM/ejY7I6N5_K8tonMx3gaPXQcjoWSLMwzJgCPcBGAYYCw/s1600/Screenshot%2B2020-01-29%2Bat%2B8.18.40%2BPM.png)

  

  

  

```
partial void BtnDirectPick\_TouchUpInside(UIButton sender)  
{
var picker = new UIDocumentPickerViewController(allowedUTIs, UIDocumentPickerMode.Open);
            picker.WasCancelled += Picker\_WasCancelled;
            picker.DidPickDocumentAtUrls += (object s, UIDocumentPickedAtUrlsEventArgs e) =>
            {
                Console.WriteLine("url = {0}", e.Urls[0].AbsoluteString);
//bool success = await MoveFileToApp(didPickDocArgs.Url);
var success = true;
string filename = e.Urls[0].LastPathComponent;
string msg = success ? string.Format("Successfully imported file '{0}'", filename) : string.Format("Failed to import file '{0}'", filename);
// Some invaild file url returns null
                NSData data = NSData.FromUrl(e.Urls[0]);
if(data!=null){
byte[] dataBytes = new byte[data.Length];

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

for (int i = 0; i < dataBytes.Length; i++)
                {
                    Console.WriteLine(dataBytes[i]);
                }
                }

                Console.WriteLine(data + "Completed");

var alertController = UIAlertController.Create("import", msg, UIAlertControllerStyle.Alert);
var okButton = UIAlertAction.Create("OK", UIAlertActionStyle.Default, (obj) =>
                {
                    alertController.DismissViewController(true, null);
                });
                alertController.AddAction(okButton);
                PresentViewController(alertController, true, null);
            };
            PresentViewController(picker, true, null);
        }

        private void Picker\_WasCancelled(object sender, EventArgs e)  
{
            Console.WriteLine("Picker was Cancelled");
        }
```

  
If you need to filter a specific format file, use below URI filters  
  

[![](https://1.bp.blogspot.com/-6fCS-D0k1EE/XjMY96GlT7I/AAAAAAAAPzI/knz-HkXlfxEXRMsC24F5Yvj1Ghj26EmGQCPcBGAYYCw/s320/Screenshot%2B2020-01-29%2Bat%2B8.18.45%2BPM.png)](https://1.bp.blogspot.com/-6fCS-D0k1EE/XjMY96GlT7I/AAAAAAAAPzI/knz-HkXlfxEXRMsC24F5Yvj1Ghj26EmGQCPcBGAYYCw/s1600/Screenshot%2B2020-01-29%2Bat%2B8.18.45%2BPM.png)

  

  

  

```
private string[] allowedUTIs = {
                    UTType.UTF8PlainText,
                    UTType.PlainText,
                    UTType.RTF,
                    UTType.PNG,
                    UTType.Text,
                    UTType.PDF,
                    UTType.Image
                };
```

### **Step 4:**

Press F5 or run the project, you will get output like below.

  

[![](https://1.bp.blogspot.com/-UogobdZRUT0/XjMY8e1AGaI/AAAAAAAAPzM/hMcn9yOzyfMij3D15xja6SqMCipivzSJwCPcBGAYYCw/s320/IMG_0708.jpg)](https://1.bp.blogspot.com/-UogobdZRUT0/XjMY8e1AGaI/AAAAAAAAPzM/hMcn9yOzyfMij3D15xja6SqMCipivzSJwCPcBGAYYCw/s1600/IMG_0708.jpg)
  

**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.