How to use the Files App in iOS

2017-09-27  本文已影响77人  sudhengshi

let's start

As we all know ,iOS 11 brings a ton of exciting new features. One of them is the new Files app,which allows users to manage files stored locally and in iCloud. In this post, we learn how your app can take advantage of this cool new feature.

So When You Want to Be a Poet

When you have created a great new app that encourages people to write more poetry. You've written several new poems and you're excited to share them.


poem-collection-3.png

And now, Each poem created within your app is stored inside your app's Documents folder as a simple text file. You're excited about the new Files app in iOS 11 and you would like to give your users the ability to manage their poems from within Files. Naturally, you jump into the Files app to start managing your files.

empty-files.png

So there must be something wrong. Your poems do not appear in the Files app. Fortunately, there's an easy fix.

Opting in with the Files App

Before your files can appear in the Files app, you must indicate that your app supports Open in Place and File Sharing Enabled. These options are configured using keys in your Info.plist file. The first key is UIFileSharingEnabled,which enables iTunes sharing of files in your Documents folder. The second key is LSSupportsOpeningDocumentsInPlace, which grants the local file provider access to files in your Documents folder. Add these keys to your Info.plist and set their values to YES.

info-plist.png

Run your app again to make these changes take effect. Congratulations! Your files should now appear in the Files app.

files-app.png

I See What You Did There

Now let's take a look at your poems in the Files app. We can see all of our poems listed, but we also see a mysterious file named'Saves'.

files-with-saves.png

Understand that any files you place in your Documents folder will be visible within the Files apps. Apple suggests you only place user-created files in the Documents folder. Placing app-created files in the Documents folder will create clutter and confound your users.

Hiding Important Files

So where should you store your app-created files? It depends on their importance. Let's say we placed the 'Saves' file in the Documents folder because we need to ensure it is backed up to iCloud. This file is important to the operation of our app, so backups are a must. This type of file should be placed in the Application Support directory instead. The Application Support directory is also backed up to iCloud, but it's contents will not appear in the Files app.Let's make this change.

// let documents =FileManager.default.urls(for: .documentDirectory,in: .userDomainMask).first!
let support = FileManager.default.urls(for: .applicationSupportDirectory,in: .userDomainMask).first!

if your file is not critical, or if it can be recreated easily, you should consider placing it somewhere else. The .cachesDirectory and FileManager.default.temporaryDirectory folders are good choices for files that aren't critical to the operation of your app.

For the More Curious

Aa part of their Fall 20117 iPhone event, Apple posted a series of WWDC-esaue videos. This post was inspired by their iOS Storage Best Practices video. The other videos cover topics like Apple TV 4K, Apple Watch Series 3, iPhone X and more.

Getting Ready for iOS 11

I hope this post helps you as you prepare your apps for the exciting new features in iOS 11. Keep an eye on my blog for other helpful iOS tips an tricks. Let's work together to get your app ready for iOS 11.

上一篇 下一篇

猜你喜欢

热点阅读