---
title: "#321: Opening Files from the Files App 📂"
url: https://littlebitesofcocoa.com/321-opening-files-from-the-files-app
author: Jake Marsh
published: 2018-12-08
topics: ["Files","UIKit"]
description: "Apple's Files.app is a great way to interact with files on iOS. Today, we'll begin taking a look at how our app can better integrate with it."
---

# #321: Opening Files from the Files App 📂

By Jake Marsh, December 7, 2018. HTML: https://littlebitesofcocoa.com/321-opening-files-from-the-files-app

[Apple's Files.app](https://support.apple.com/en-us/HT206481) is a great way to interact with _files_ on iOS.

Today, we'll begin taking a look at how our app can better integrate with it. First up, we're going to learn how to allow users to open files in our app, <i>from</i> the _Files.app_. Let's get started.

We'll begin by creating a new project with the _Single View App_ template in Xcode. In the future, we'll look at how <i>Document Picker</i> apps work, but for simplicity, we'll use just a plain <i>single view</i> app this time.

Then, we'll head into our `AppDelegate.swift` file, and add a new **function**:

``` swift
func application(
  _ app: UIApplication, 
  open inputURL: URL, 
  options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
  // TODO
}
```

This `application(_:open:options:)` **function** will be called when the user selects a file inside the _Files app_ and presses the Share (&nbsp;<img src="https://media.littlebitesofcocoa.com/321-action-glyph.png" style="width: 19px !important" />&nbsp;) button on it, and selects our app in the `UIActivityViewController` that appears.

**Pro Tip:** We can also access this by long-pressing a file in the _Files app_ and selecting **Share** from the menu:

<img src="https://media.littlebitesofcocoa.com/321-3.png" width="500" />

Next we'll need to tell Xcode that our app supports the type of files we want to our app to be able to open. For this, we'll choose <i>Text Files</i>.

We'll click on our **project** at the top of the **project navigator** and click **Info** at the top. We'll find this screen:

<img src="https://media.littlebitesofcocoa.com/321-1.png" width="500" />

We want that area labeled **Document Types (0)**. We'll click the **+** button and enter in `Text` for the **Name** and `public.text` for **Types**.

<img src="https://media.littlebitesofcocoa.com/321-2.png" width="500" />

(This `public.text` thing is part of a system called [Uniform Type Identifiers](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html), Apple uses these to identify specific <i>kinds</i> of file types. [Read more here](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html)).

Then, we'll add a new key to our Info.plist to make our app work more smoothly. We'll right-click on the keys at the top and select **Add Row**, then add the key `LSSupportsOpeningDocumentsInPlace`. We'll make it a **Boolean** and set its value to `YES`:

<img src="https://media.littlebitesofcocoa.com/321-6.png" width="500" />

This allows our app to work with the files users open, without needing to first copy them into its own sandbox directory.

<p class='break'></p>

Finally, we'll head back to our `AppDelegate.swift` file and fill out that **function** we added earlier:

``` swift
func application(
  _ app: UIApplication, 
  open url: URL, 
  options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
  let needTo = url.startAccessingSecurityScopedResource()

  do {
    let data = try Data(contentsOf: url)
    let contents = String(
      data: data,
      encoding: String.Encoding.utf8
    )

    print(contents)
  } catch(let error) { print(error) }

  if needTo {
    url.stopAccessingSecurityScopedResource()
  }

  return true
}

```

First we setup a `do/catch` block so we can catch and print any errors that might happen during all of this. Then we call a special function on our `inputURL`.

Since, we're <i>opening in place</i>, iOS has gives us what Apple calls a "security scoped" URL. ([Read more about them here](https://developer.apple.com/documentation/foundation/nsurl#1663809)).

First we have to tell the system when we begin using a URL, saving the `Bool` value that `url.startAccessingSecurityScopedResource()` returns. If that `Bool` is `true`, then we'll also need to call `stopAccessingSecurityScopedResource` on our URL once we're finished with it.

After that we simply read the `Data` at the URL, then put it into a `String` so we can print it out.

Now we can Build & Run the app on our device, open the Files.app, choose an `.txt` file, share it, and our app appears in the **Share sheet**:

<img src="https://media.littlebitesofcocoa.com/321-5.png" width="375" />

Tapping our app's icon opens it, and runs our code to print out the contents of the file, neat!

<img src="https://media.littlebitesofcocoa.com/321-7.png" width="415" />

That's all for today. Shout out to friend of the show [Casey](http://twitter.com/caseyliss) for [inspiring the idea for this Bite](https://twitter.com/caseyliss/status/1071227489505157120)!

Have an idea for a Bite? [Send it in!](mailto:hello@littlebitesofcocoa.com)

<p></p>
