•Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session...

61
#WWDC17 © 2017 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. Dave Rahardja, UIKit Tanu Singhal, UIKit Data Delivery with Drag and Drop Session 227 App Frameworks

Transcript of •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session...

Page 1: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

#WWDC17

© 2017 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Dave Rahardja, UIKit Tanu Singhal, UIKit

•Data Delivery with Drag and Drop • Session 227

App Frameworks

Page 2: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

API Roadmap

ItemProvider

DropSession

DragSession

DragInteraction

Drag Interaction Delegate Drag Item

Preview Drop Interaction

Drop Interaction Delegate

Page 3: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

API Roadmap

ItemProvider

DropSession

DragSession

DragInteraction

Drag Interaction Delegate Drag Item

Preview Drop Interaction

Drop Interaction Delegate

Page 4: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

•NSItemProvider Basics•Uniform Type Identifiers•Model Classes•Advanced Topics

Page 5: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

•NSItemProvider Basics

Page 6: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProvider

Data promises

Asynchronous

Progress, cancellable

Supported • Drag and Drop • UIPasteConfiguration • UIPasteboard

Page 7: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Data Promise

Page 8: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Data Promise

Providing Data

let image = UIImage(named: "Photo")

let itemProvider = NSItemProvider(object: image!)

Page 9: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Data Promise

Providing Data

let image = UIImage(named: "Photo")

let itemProvider = NSItemProvider(object: image!)

Retrieving Data

itemProvider.loadObject(ofClass: UIImage.self) { (object, error) in

if let image = object as? UIImage {

// use image } }

Page 10: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Data Promise

Providing Data

let image = UIImage(named: "Photo")

let itemProvider = NSItemProvider(object: image!)

Retrieving Data

itemProvider.loadObject(ofClass: UIImage.self) { (object, error) in

if let image = object as? UIImage {

DispatchQueue.main.async {

// Update UI

} }

}

Page 11: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

•Demo

Page 12: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Progress and Cancellation

Page 13: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Progress and Cancellation

Retrieving Data

let progress = itemProvider.loadObject(ofClass: UIImage.self) { // Returns progress

(object, error) in

// ...

}

let progressSoFar = progress.fractionCompleted

let isFinished = progress.isFinished

progress.cancel()

Page 14: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Progress and Cancellation

One Progress object per load request

Overall Progress object from UIDropSession

Page 15: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Maximize Compatibility

Page 16: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

•Uniform Type Identifiers

Page 17: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Multiple Representations

One NSItemProvider = one “thing” being dragged

Multiple representations

Page 18: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Multiple Representations

One NSItemProvider = one “thing” being dragged

Multiple representations

Vector drawing • Native file format • PDF • PNG • JPG

Page 19: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Multiple Representations

One NSItemProvider = one “thing” being dragged

Multiple representations

Vector drawing • Native file format — com.yourcompany.vector-drawing • PDF — com.adobe.pdf • PNG — public.png • JPG — public.jpeg

Page 20: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Multiple Representations

One NSItemProvider = one “thing” being dragged

Multiple representations

Vector drawing • Native file format — com.yourcompany.vector-drawing • PDF — kUTTypePDF • PNG — kUTTypePNG • JPG — kUTTypeJPEG

Page 21: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Fidelity Order

Highest fidelity first • Internal type • Highest fidelity common type • Next highest fidelity common type • …

Page 22: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Use Concrete Uniform Type Identifiers

Page 23: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Use Concrete Uniform Type Identifiers

Abstract item • public.data • public.plain-text • public.image

Page 24: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Use Concrete Uniform Type Identifiers

Abstract item • public.data • public.plain-text • public.image

Page 25: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Use Concrete Uniform Type Identifiers

Abstract item • public.data • public.plain-text • public.image

Concrete data • public.utf8-plain-text • public.png

Page 26: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Use Concrete Uniform Type Identifiers

Abstract item • public.data • public.plain-text • public.image

Concrete data • public.utf8-plain-text • public.png

Private type identifier • com.yourcompany.vector-drawing

Page 27: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

•Model Classes

Page 28: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderReading, -Writing Protocols

Page 29: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderReading, -Writing Protocols

NSItemProviderWriting — exports data from model object

Page 30: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderReading, -Writing Protocols

NSItemProviderWriting — exports data from model object

NSItemProviderReading — creates model object from data

Page 31: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderReading, -Writing Protocols

NSItemProviderWriting — exports data from model object

NSItemProviderReading — creates model object from data

Maintained with model classes, not UI code

Page 32: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderWriting Protocol

Page 33: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderWriting Protocol

public protocol NSItemProviderWriting : NSObjectProtocol { public static var writableTypeIdentifiersForItemProvider: [String] { get } public func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? }

Page 34: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderWriting Protocol

public protocol NSItemProviderWriting : NSObjectProtocol { public static var writableTypeIdentifiersForItemProvider: [String] { get } public func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? }

let itemProvider = NSItemProvider(object: vectorObject)

Page 35: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderWriting Protocol

public protocol NSItemProviderWriting : NSObjectProtocol { public static var writableTypeIdentifiersForItemProvider: [String] { get } public func loadData(withTypeIdentifier typeIdentifier: String, forItemProviderCompletionHandler completionHandler: @escaping (Data?, Error?) -> Void) -> Progress? }

itemProvider.registerDataRepresentation(forTypeIdentifier: "com.yourcompany.vector-drawing" …) itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypePDF …) itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypeTIFF …) itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypePNG …) itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypeJPEG …)

let itemProvider = NSItemProvider(object: vectorObject)

Page 36: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderReading Protocol

Page 37: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderReading Protocol

public protocol NSItemProviderReading : NSObjectProtocol {

public static var readableTypeIdentifiersForItemProvider: [String] { get }

public init(itemProviderData data: Data, typeIdentifier: String) throws

}

Page 38: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

NSItemProviderReading Protocol

public protocol NSItemProviderReading : NSObjectProtocol {

public static var readableTypeIdentifiersForItemProvider: [String] { get }

public init(itemProviderData data: Data, typeIdentifier: String) throws

}

if itemProvider.canLoadObject(ofClass: VectorDrawing.self) {

itemProvider.loadObject(ofClass: VectorDrawing.self) {

(object, error) in //...

}

}

Page 39: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Model Classes

Conform to NSItemProviderReading, Writing

NSObject

Drag and Drop, UIPasteConfiguration, UIPasteboard

Page 40: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

•Demo

Page 41: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

•Advanced Topics

Page 42: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Data Marshaling

Page 43: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Data Marshaling

Providing • As NSData itemProvider.registerDataRepresentation(…)

• As a file or folder itemProvider.registerFileRepresentation(…fileOptions:[])

• File Provider (open in place optional) itemProvider.registerFileRepresentation(…fileOptions:[.openInPlace])

Page 44: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Data Marshaling

Retrieving • Copy as NSData itemProvider.loadDataRepresentation(…)

• Copy as file or folder itemProvider.loadFileRepresentation(…)

• Attempt to open in place itemProvider.loadInPlaceFileRepresentation(…) // Falls back to file copy

Page 45: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Data Marshaling

File → NSData

NSData → File copy

Folder → NSData (zipped)

File Provider → File copy

Page 46: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Return your own Progress

Progress and Cancellation

Page 47: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Return your own Progress func loadData(withTypeIdentifier typeIdentifier: String,

forItemProviderCompletionHandler completionHandler:

@escaping (Data?, Error?) -> Void) -> Progress? {

let dataLoader = DataLoader()

let progress = Progress(totalUnitCount: 100)

var shouldContinue = true

progress.cancellationHandler = {

shouldContinue = false

}

dataLoader.beginLoading(update: { percentDone in

progress.completedUnitCount = percentDone

return shouldContinue

}, completionHandler: completionHandler)

return progress

}

Progress and Cancellation

Page 48: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Return your own Progress func loadData(withTypeIdentifier typeIdentifier: String,

forItemProviderCompletionHandler completionHandler:

@escaping (Data?, Error?) -> Void) -> Progress? {

let dataLoader = DataLoader()

let progress = Progress(totalUnitCount: 100)

var shouldContinue = true

progress.cancellationHandler = {

shouldContinue = false

}

dataLoader.beginLoading(update: { percentDone in

progress.completedUnitCount = percentDone

return shouldContinue

}, completionHandler: completionHandler)

return progress

}

Progress and Cancellation

Page 49: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Return your own Progress func loadData(withTypeIdentifier typeIdentifier: String,

forItemProviderCompletionHandler completionHandler:

@escaping (Data?, Error?) -> Void) -> Progress? {

let dataLoader = DataLoader()

let progress = Progress(totalUnitCount: 100)

var shouldContinue = true

progress.cancellationHandler = {

shouldContinue = false

}

dataLoader.beginLoading(update: { percentDone in

progress.completedUnitCount = percentDone

return shouldContinue

}, completionHandler: completionHandler)

return progress

}

Progress and Cancellation

Page 50: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Return your own Progress func loadData(withTypeIdentifier typeIdentifier: String,

forItemProviderCompletionHandler completionHandler:

@escaping (Data?, Error?) -> Void) -> Progress? {

let dataLoader = DataLoader()

let progress = Progress(totalUnitCount: 100)

var shouldContinue = true

progress.cancellationHandler = {

shouldContinue = false

}

dataLoader.beginLoading(update: { percentDone in

progress.completedUnitCount = percentDone

return shouldContinue

}, completionHandler: completionHandler)

return progress

}

Progress and Cancellation

Page 51: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Per-Representation Visibility

Restrict visibility • Same application • Same Team • Everyone

Use to hide private types

Page 52: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Team Data

teamData property

Up to 8 KB of metadata

Visible to same Team

Improve UI during drag

Page 53: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Suggested Name

suggestedName property

Used as file name

Use when providing NSData

Page 54: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

preferredPresentationSize property

Use to target drop animation

Preferred Presentation Size

Page 55: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

File Provider

App extension

Allows app to be terminated

URL to file in File Provider container

Open in Place

Building Great Document-based Apps in iOS 11 Hall 2 Thursday 1:50PM

File Provider Enhancements Hall 3 Friday 11:00AM

Page 56: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

•Demo

Page 57: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Summary

NSItemProvider • Multiple representations • Asynchronous • Progress, cancellable

NSItemProviderReading and Writing

Visibility and Team Data

File Provider, Open in Place

Page 58: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

More Informationhttps://developer.apple.com/wwdc17/227

Page 59: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Related Sessions

Introducing Drag and Drop Hall 3 Tuesday 11:20AM

Mastering Drag and Drop Exec Ballroom Wednesday 11:00AM

Drag and Drop with Collection and Table View Hall 2 Thursday 9:00AM

Building Great Document-based Apps in iOS 11 Hall 2 Thursday 1:50PM

File Provider Enhancements Hall 3 Friday 11:00AM

Page 60: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop

Labs

Cocoa Touch and Haptics Lab Technology Lab C Fri 12:00PM–1:10PM

Page 61: •Data Deliver y with Drag and Drop€¦ · API Roadmap Item Provider Drop Session Drag Session Drag Interaction Drag Interaction Delegate Drag Item Preview Drop Interaction Drop