© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
#WWDC15
Building Document Based Apps
Mike Hess Software EngineerJohannes Fortmann Senior Software Engineer
App Frameworks
Session 234
Document is a single standalone entity Document based app manages a list of documents
What’s a Document Based App?General characteristics
Document is a single standalone entity Document based app manages a list of documentsPresents to user to edit etc
What’s a Document Based App?General characteristics
Document is a single standalone entity Document based app manages a list of documentsPresents to user to edit etc
What’s a Document Based App?General characteristics
Document is a single standalone entity Document based app manages a list of documentsPresents to user to edit etc
What’s a Document Based App?General characteristics
Document is a single standalone entity Document based app manages a list of documentsPresents to user to edit etc
What’s a Document Based App?General characteristics
Document BrowserComponents
List documents in a meaningful wayUse thumbnails for document visibilityShow external documents
Document BrowserComponents
List documents in a meaningful wayUse thumbnails for document visibilityShow external documentsStore recently accessed documents
Document BrowserDiscovering documents
NSFileManager results are incomplete• iCloud content isn’t always available locally
Document BrowserDiscovering documents
NSFileManager results are incomplete• iCloud content isn’t always available locally• External documents not included
Document BrowserDiscovering documents
Doc 1
Doc 2
Doc 4
Doc 5
Doc 3
Your App’s Container
Other App’s Container
NSURL 1
NSURL 2
NSFileManager APIs
Document BrowserDiscovering documents
Doc 1
Doc 2
Doc 4
Doc 5
Doc 3
Your App’s Container
Other App’s Container
NSURL 1
NSURL 2
NSFileManager APIs
Document BrowserDiscovering documents
Doc 1
Doc 2
Doc 4
Doc 5
Doc 3
Your App’s Container
Other App’s Container
Document BrowserDiscover documents with NSMetadataQuery
Doc 1
Doc 2
Doc 4
Doc 5
Doc 3
Your App’s Container
Other App’s Container
NSMetadataQuery APIs
Document BrowserDiscover documents with NSMetadataQuery
NSMetadataItem 1
NSMetadataItem 3
NSMetadataItem 2
NSMetadataItem 4
Doc 1
Doc 2
Doc 4
Doc 5
Doc 3
Your App’s Container
Other App’s Container
NSMetadataQuery APIs
Document BrowserDiscover documents with NSMetadataQuery
NSMetadataItem 1
NSMetadataItem 3
NSMetadataItem 2
NSMetadataItem 4
Doc 1
Doc 2
Doc 4
Doc 5
Doc 3
Your App’s Container
Other App’s Container
Document Browser
Doc 1
Doc 2
Doc 3
Your App’s Container
Other App’s Container
NSMetadataQuery
Doc 4
Doc 5
Document Browser
Doc 1
Doc 2
Doc 3
Your App’s Container
Other App’s Container
NSMetadataQuery
Create NSMetadataQuery
Doc 4
Doc 5
NSMetadataItem 2
NSMetadataItem 3
NSMetadataItem 4
NSMetadataItem 1
Document Browser
Doc 1
Doc 2
Doc 3
Your App’s Container
Other App’s Container
NSMetadataQuery
Initial Gather Phase
Doc 4
Doc 5
NSMetadataItem 2
NSMetadataItem 3
NSMetadataItem 4
NSMetadataItem 1
Document Browser
Doc 1
Doc 2
Doc 3
Your App’s Container
Other App’s Container
NSMetadataQuery
List Initial Documents
Doc 4
Doc 5
Doc 6
NSMetadataItem 2
NSMetadataItem 6
NSMetadataItem 3
NSMetadataItem 4
NSMetadataItem 1
Document Browser
Doc 1
Doc 2
Doc 3
Your App’s Container
Other App’s Container
NSMetadataQuery
Update Notifications
Doc 4
Doc 5
Doc 6
NSMetadataItem 2
NSMetadataItem 6
NSMetadataItem 3
NSMetadataItem 4
NSMetadataItem 1
Document Browser
Doc 1
Doc 2
Doc 3
Your App’s Container
Other App’s Container
NSMetadataQuery
Compute Animations
Doc 4
Doc 5
Doc 6
NSMetadataItem 2
NSMetadataItem 6
NSMetadataItem 3
NSMetadataItem 4
NSMetadataItem 1
Document Browser
Doc 1
Doc 2
Doc 3
Your App’s Container
Other App’s Container
NSMetadataQuery
Display Updated UI
Doc 4
Doc 5
Document BrowserThumbnails
Gives visual context to usersGenerated automatically for certain documents
Document BrowserLoading thumbnails for display
Main Queue
BackgroundQueue
Display CellWith PlaceholderLoad Cell
Fetch Thumbnail
Document BrowserLoading thumbnails for display
Main Queue
BackgroundQueue
Reload CellDisplay CellWith PlaceholderLoad Cell
Fetch Thumbnail
Document BrowserLoading thumbnails for display
Main Queue
BackgroundQueue
Reload CellDisplay CellWith Placeholder
Display CellWith ThumbnailLoad Cell
Fetch Thumbnail
Recents ListStoring recently-used items list
Give easy access to recently used documentsStoring NSURLs isn’t robust
Document Browser Summary
Discover documents using NSMetadataQueryDisplay thumbnailsStore recent list using bookmarks
Document AccessFile coordination and presentation
Multiple applications can read or write documents• Your app, other apps, iCloud Drive…
Write
Document AccessFile coordination and presentation
WriteYour App
Other App ReadInconsistent Read
Write
Stale Display
Document AccessFile coordination and presentation
WriteYour App
Other App ReadInconsistent Read
Stale Display
Write
Document Access
Your App
Other App
Write
File coordination and presentation
Inconsistent Read
NSFileCoordination: Reader/writer lock
Stale Display
Write
Document Access
Your App
Other App
Write
File coordination and presentation
Inconsistent Read
NSFileCoordination: Reader/writer lock
Stale Display
Write
Document Access
Your App
Other App
Write
File coordination and presentation
Coordinated Read
NSFileCoordination: Reader/writer lockNSFilePresenter: Notification mechanism
Stale Display
Write
Document Access
Your App
Other App
Write
File coordination and presentation
Coordinated Read
NSFileCoordination: Reader/writer lockNSFilePresenter: Notification mechanism
Update
Write
Document Access
Your App
Other App
Write
File coordination and presentation
Coordinated Read
Goals• Consistent• Up-to-date
Document ManagementCreating new documents
Document CreationYour App Display in UI
Goals• Consistent• Up-to-date
Document ManagementCreating new documents
Document CreationYour App Display in UI
Coordinated operations update directly!
Document Management
Display in UIDocument CreationYour App
Creating new documents
Coordinated operations update directly!
Document Management
Display in UIDocument CreationYour App
Creating new documents
Coordinated operations can block
Document Management
Mainthread Main Thread Blocked
Creating new documents
Document Creation
Coordinated operations can blockUse background queue
Document Management
Mainthread Main Thread Blocked
Backgroundqueue
Creating new documents
Document Creation
Coordinated operations can blockUse background queue
Document Management
Mainthread
Backgroundqueue
User action
Creating new documents
Document Creation
Coordinated operations can blockUse background queue
Document Management
Mainthread
Display in UI
Backgroundqueue
User action
Creating new documents
Document Creation
Coordinated operations can blockUse background queueDeletion same
Document Management
Mainthread
Display in UI
Backgroundqueue
User action
Creating new documents
Document Creation
UIDocument for reading and writing
Mainthread
Backgroundqueue
Loading and DisplayingReading documents
UIDocument for reading and writing
Mainthread
Backgroundqueue
Loading and DisplayingReading documents
open
UIDocument for reading and writing
Mainthread
Backgroundqueue
Loading and DisplayingReading documents
open
Coordinated Read
UIDocument for reading and writing
Mainthread
Backgroundqueue
Loading and DisplayingReading documents
open
Coordinated Read
loadFromContents()
UIDocument for reading and writing
Mainthread
Backgroundqueue
Loading and DisplayingReading documents
open
Coordinated Read
loadFromContents()
completion
Mainthread
Backgroundqueue
Coordinated Read
loadFromContents()
Loading and DisplayingReading documents
open completion
Reading may trigger a download
Mainthread
Backgroundqueue
Coordinated Read
loadFromContents()
Loading and DisplayingReading documents
open completion
Reading may trigger a downloadNSProgressReporting
Mainthread
Backgroundqueue
Coordinated Read
loadFromContents()
Loading and DisplayingReading documents
open completion
Reading may trigger a downloadNSProgressReporting
Mainthread
Backgroundqueue
Coordinated Read
loadFromContents()
Loading and DisplayingReading documents
open State Change completion
Reading may trigger a downloadNSProgressReporting
Mainthread
Backgroundqueue
Coordinated Read
loadFromContents()
Loading and DisplayingReading documents
open State Change Progress display completion
Reading may trigger a downloadNSProgressReporting
Mainthread
Backgroundqueue
Coordinated Read
loadFromContents()
Loading and DisplayingReading documents
open State Change State ChangeProgress display completion
Loading and DisplayingWriting documents
Like reading: File coordination, background queue
Coordinated Write
contentsForType()Background
queue
Loading and DisplayingWriting documents
Like reading: File coordination, background queueThumbnails
Coordinated Write
contentsForType() fileAttributesToWriteToURL()Background
queue
Loading and DisplayingWriting documents
Like reading: File coordination, background queueThumbnailsCalled on a background queue!
Coordinated Write
contentsForType() fileAttributesToWriteToURL()Background
queue
Loading and DisplayingWriting documents
Like reading: File coordination, background queueThumbnailsCalled on a background queue!• No UIViews!
Coordinated Write
contentsForType() fileAttributesToWriteToURL()Background
queue
iOS 8—Open by copy
Apps don’t have access to each others documents• Opening using the UIDocumentInteractionController causes a copy
Your App’s Container Other App's Container
Opening from Other Apps
iOS 8—Open by copy
Apps don’t have access to each others documents• Opening using the UIDocumentInteractionController causes a copy
Your App’s Container Other App's Container
Doc 1_v1
Doc 1_v2
Opening from Other Apps
iOS 8—Open by copy
Apps don’t have access to each others documents• Opening using the UIDocumentInteractionController causes a copy• Proliferation of copies
Your App’s Container Other App's Container
Doc 1_v1
Doc 1_v3
Doc 1_v2
Opening from Other Apps
Your App’s Container Other App's Container
Doc 1_v1
iOS 9—Open in place
Access to other app’s containers granted by document interaction controller
Opening from Other Apps
Your App’s Container Other App's Container
Doc 1_v1
Doc 1_v1
iOS 9—Open in place
Access to other app’s containers granted by document interaction controller
Opening from Other Apps
Your App’s Container Other App's Container
Doc 1_v1
Doc 1_v1
iOS 9—Open in place
Access to other app’s containers granted by document interaction controller• Includes files opened from the iCloud Drive app and Spotlight
Opening from Other Apps
Your App’s Container Other App's Container
Doc 1_v1
Doc 1_v1
iOS 9—Open in place
Access to other app’s containers granted by document interaction controller• Includes files opened from the iCloud Drive app and Spotlight• UIDocumentMenuViewController, UIDocument
Opening from Other Apps
Opening from Other AppsSupporting open in place
Support is simple• LSSupportsOpeningDocumentsInPlace key in Info.plist
Opening from Other AppsSupporting open in place
Support is simple• LSSupportsOpeningDocumentsInPlace key in Info.plist• Adopt new delegate method
Handling open in place versus copy
func application(app: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { let newURL = copyFileToContainer(url) documentBrowser.openDocumentAtURL(newURL) return true }
Opening from Other Apps
Handling open in place versus copy
func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool { let newURL = copyFileToContainer(url) documentBrowser.openDocumentAtURL(newURL) return true }
Opening from Other Apps
Handling open in place versus copy
func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool { guard let shouldOpenInPlace = options[UIApplicationOpenURLOptionsOpenInPlaceKey] as? Bool else { return false }
let newURL = copyFileToContainer(url) documentBrowser.openDocumentAtURL(newURL)
return true }
Opening from Other Apps
Handling open in place versus copy
func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool { guard let shouldOpenInPlace = options[UIApplicationOpenURLOptionsOpenInPlaceKey] as? Bool else { return false }
let newURL = shouldOpenInPlace ? url : copyFileToContainer(url) documentBrowser.openDocumentAtURL(newURL)
return true }
Opening from Other Apps
Handling open in place versus copy
func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool { guard let shouldOpenInPlace = options[UIApplicationOpenURLOptionsOpenInPlaceKey] as? Bool else { return false }
let newURL = shouldOpenInPlace ? url : copyFileToContainer(url) documentBrowser.openDocumentAtURL(newURL)
return true }
Opening from Other Apps
Summary
Make your app performant, responsive, and beautiful• NSMetadataQuery• Bookmarks• Progress• Open-in-place
More Information
Sample Codehttp://developer.apple.com/library/prerelease/ios/samplecode/ShapeEdit
Technical SupportApple Developer Forumshttp://developer.apple.com/forums
iCloud Documentationhttp://developer.apple.com/icloud