WWDC 2015

Building document based app

Related Session : Building Document Based app WWDC 2014

What is document based app

  • standalone
  • manages a list of documents
  • presents to user to edit etc.

Document browser

  • list doc in a manageful way
  • use thumbnails to preview the doc
  • show external doc like doc in iCloud
  • show recently accessed documents

Discovering document

NSFileManager only list the local doc, not for doc on iCloud or doc in other apps

NSMetadataQuery combines all available including local ,remote and external doc from other apps. This populate the notification to inform hosting app for getting remote doc and any updates to the doc.

Thumbnails are generated automatically for some types of documents.

Recent List contains the document of user just worked on. Storing NSURL is not robust as the doc may be moved. Use security scoped bookmarks as a pointer to the doc

Document Assess

NSFileCoordination for read/write lock NSFilePresenter to receive the updates whenever the document has been modified from other parties.

Creating/Deleting new documents

Use background queue to create new doc as coordinated operations can block

Loading and Displaying documents

Reading

Strongly suggested to use UIDocument to read and write, invoke loadFromContents() in background thread and completion on main queue

in iOS 9

  • Reading may requires downloading
  • NSProgressReporting on UIDocument protocol is used for telling how much work done.

Writing

Content and thumbnails are done on background threads.

if providing custom thumbnails, make sure to thread-safety classes to create the images. UIViews are not thread-safe. Use Core graphic suggested.

Opening from other app

in iOS 8, the doc from other apps are copies. Presenting multiple copies to user is confusing.

in iOS 9, the doc may be directly reference to the another app documents as known as Open in place via UIDocumentMenuViewController and UIDocument

Support Open in place :

  • add LSSupportsOpeningDocumentsInPlace key in info.plist
  • adopting new delegate methods
func application(app: UIApplication, openUrl url: NSURL, options: [String: AnyObject]) -> Bool {
    guard let shouldOpenInPlace = options[UIApplicationOpenURLOptionsOpenInPlaceKey] as? Bool else {
        return false
    }
    //use the external url
    let newURL = shouldOpenInPlace ? url : copyFileToContain(url)
    documentBrowser.openDocumentAtURL(newURL)

    return true
}