Norway


Topics

Topics

Appleโ€™s Files.app is a 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, from 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 Document Picker apps work, but for simplicity, weโ€™ll use just a plain single view app this time.

Then, weโ€™ll head into our AppDelegate.swift file, and add a new function:

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 (ย - 321 action glyph - #321: Opening Files from the Files App ๐Ÿ“‚ – Little Bites of Cocoaย ) 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:

- 321 3 - #321: Opening Files from the Files App ๐Ÿ“‚ – Little Bites of Cocoa

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 Text Files.

Weโ€™ll click on our project at the top of the project navigator and click Info at the top. Weโ€™ll find this screen:

- 321 1 - #321: Opening Files from the Files App ๐Ÿ“‚ – Little Bites of Cocoa

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.

- 321 2 - #321: Opening Files from the Files App ๐Ÿ“‚ – Little Bites of Cocoa

(This public.text thing is part of a system called Uniform Type Identifiers, Apple uses these to identify specific kinds of file types. Read more here).

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:

- 321 6 - #321: Opening Files from the Files App ๐Ÿ“‚ – Little Bites of Cocoa

This allows our app to work with the files users open, without needing to first copy them into its own sandbox directory.

Finally, weโ€™ll head back to our AppDelegate.swift file and fill out that function we added earlier:

func application(
  _ app: UIApplication, 
  open url: URL, 
  options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
  let needTo = url.startAccessingSecurityScopedResource()

  do {
    let  = 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 opening in place, iOS has gives us what Apple calls a โ€œ scopedโ€ URL. (Read more about them here).

First we have to tell the system when we begin using a URL, 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:

- 321 5 - #321: Opening Files from the Files App ๐Ÿ“‚ – Little Bites of Cocoa

Tapping our appโ€™s icon opens it, and runs our code to print out the contents of the file, neat!

- 321 7 - #321: Opening Files from the Files App ๐Ÿ“‚ – Little Bites of Cocoa

Thatโ€™s all for today. Shout out to friend of the show Casey for inspiring the idea for this Bite!

Have an idea for a Bite? Send it in!





Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here