Slide 1

Slide 1 text

Extensions in iOS 8 (like peanut butter and chocolate)

Slide 2

Slide 2 text

Ben Scheirman @subdigital benscheirman.com

Slide 3

Slide 3 text

ChaiOne Houston, TX @chaione

Slide 4

Slide 4 text

NSScreencast

Slide 5

Slide 5 text

Extensions!

Slide 6

Slide 6 text

Your App + Other Apps

Slide 7

Slide 7 text

Your App + 's Apps

Slide 8

Slide 8 text

Extension Types Available

Slide 9

Slide 9 text

Today Extension Add Widget to Notification Center

Slide 10

Slide 10 text

Share Extension Share Content to Your App

Slide 11

Slide 11 text

Action Extension Act Upon Content

Slide 12

Slide 12 text

Keyboard Photo Editing Document Provider Not covered here :(

Slide 13

Slide 13 text

Extension Architecture

Slide 14

Slide 14 text

Isolated Process

Slide 15

Slide 15 text

Independent Lifecycle

Slide 16

Slide 16 text

Delivered with Applications

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Extensions are Just View Controllers

Slide 20

Slide 20 text

The Ever-Important Info.plist

Slide 21

Slide 21 text

NSExtension top level dictionary

Slide 22

Slide 22 text

NSExtensionAttributes nested dictionary

Slide 23

Slide 23 text

NSExtensionPrincipalClassName Provide a class to instantiate if not using storyboards.

Slide 24

Slide 24 text

NSExtensionMainStoryboard Provide the name of your extension's storyboard.

Slide 25

Slide 25 text

NSExtensionPointIdentifier Indicates the type of extension to the OS.

Slide 26

Slide 26 text

What is an Activation Rule?

Slide 27

Slide 27 text

Supported Activation Rules NSExtensionActivationSupportsAttachmentsWithMaxCount NSExtensionActivationSupportsAttachmentsWithMinCount NSExtensionActivationSupportsFileWithMaxCount NSExtensionActivationSupportsImageWithMaxCount NSExtensionActivationSupportsMovieWithMaxCount NSExtensionActivationSupportsText NSExtensionActivationSupportsWebURLWithMaxCount NSExtensionActivationSupportsWebPageWithMaxCount

Slide 28

Slide 28 text

or a Predicate

Slide 29

Slide 29 text

Default is TRUEPREDICATE

Slide 30

Slide 30 text

Don't Ship with this!

Slide 31

Slide 31 text

SUBQUERY ( extensionItems, $extensionItem, SUBQUERY ( $extensionItem.attachments, $attachment, ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf" ).@count == $extensionItem.attachments.@count ).@count == 1

Slide 32

Slide 32 text

Info.plist Lots more options, read: https://developer.apple.com/library/prerelease/ iOS/documentation/General/Reference/ InfoPlistKeyReference/Articles/ SystemExtensionKeys.html

Slide 33

Slide 33 text

Sharing Code Between App and Extensions

Slide 34

Slide 34 text

Easy way: just add a class to multiple targets!

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Though, you probably want to create a Framework

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Sharing Storyboards?

Slide 40

Slide 40 text

Let's build a today extension

Slide 41

Slide 41 text

Step 1: Add Extension Target

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Step 2: Design the Widget

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Step 3: Share Common Code

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Step 4: Wire it up

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

Let's see it in action!

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

How do I keep them in sync?

Slide 52

Slide 52 text

Sharing Data between App and Extension

Slide 53

Slide 53 text

Can Share: · NSUserDefaults · CoreData · Files · Background Transfer Sessions

Slide 54

Slide 54 text

Create an App Group must start with "group."

Slide 55

Slide 55 text

Create for containing app Create same group for extension

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Setup Shared NSUserDefaults var userDefaults: NSUserDefaults { get { return NSUserDefaults(suiteName: "group.com.nsscreencast.coffeetracker") } }

Slide 58

Slide 58 text

Add to CoffeeTracker var numberOfCups: Int { get { return userDefaults.integerForKey("cups") } set { userDefaults.setInteger(newValue, forKey: "cups") updateDisplay() } }

Slide 59

Slide 59 text

Handle Periodic Background Updates func widgetPerformUpdateWithCompletionHandler(completionHandler: (NCUpdateResult) -> ()) { coffeeTracker.updateDisplay() // If an error is encountered, use NCUpdateResult.Failed // If there's no update required, use NCUpdateResult.NoData // If there's an update, use NCUpdateResult.NewData completionHandler(NCUpdateResult.NewData) }

Slide 60

Slide 60 text

Share Extensions

Slide 61

Slide 61 text

· Share content to another app / service · Use System Sharing UI or Custom · Upload using shared background session

Slide 62

Slide 62 text

Introducing... FoodBook

Slide 63

Slide 63 text

Step 1: Add a framework for our shared code

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Useful for putting networking code or view controllers common to app and extension

Slide 66

Slide 66 text

Step 2: Add a Share Extension Target

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Step 3: Update Info.plist

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

Step 4: Edit View Controller

Slide 72

Slide 72 text

(brace yourself)

Slide 73

Slide 73 text

Import MobileCoreServices import MobileCoreServices

Slide 74

Slide 74 text

Add a property to store the image being shared var image: UIImage?

Slide 75

Slide 75 text

Get the Item Provider override func viewDidLoad() { var itemProvider: NSItemProvider? }

Slide 76

Slide 76 text

Get the Item Provider override func viewDidLoad() { var itemProvider: NSItemProvider? if let items = extensionContext?.inputItems { } }

Slide 77

Slide 77 text

Get the Item Provider override func viewDidLoad() { var itemProvider: NSItemProvider? if let items = extensionContext?.inputItems { if let item = items.first as? NSExtensionItem { } } }

Slide 78

Slide 78 text

Get the Item Provider override func viewDidLoad() { var itemProvider: NSItemProvider? if let items = extensionContext?.inputItems { if let item = items.first as? NSExtensionItem { if let attachment = item.attachments?.first as? NSItemProvider { itemProvider = attachment } } } }

Slide 79

Slide 79 text

Pull an image out of the item provider override func viewDidLoad() { // ... let imageType = kUTTypeImage as NSString if itemProvider?.hasItemConformingToTypeIdentifier(imageType) == true { } }

Slide 80

Slide 80 text

Pull an image out of the item provider override func viewDidLoad() { // ... let imageType = kUTTypeImage as NSString if itemProvider?.hasItemConformingToTypeIdentifier(imageType) == true { itemProvider?.loadItemForTypeIdentifier(imageType, options: nil) { (item, error) in }) } }

Slide 81

Slide 81 text

Pull an image out of the item provider override func viewDidLoad() { // ... let imageType = kUTTypeImage as NSString if itemProvider?.hasItemConformingToTypeIdentifier(imageType) == true { itemProvider?.loadItemForTypeIdentifier(imageType, options: nil) { (item, error) in if error == nil { let url = item as NSURL let imageData = NSData(contentsOfURL: url) self.image = UIImage(data: imageData) } else { println("ERROR: \(error)") } }) } }

Slide 82

Slide 82 text

Step 5: Perform the upload

Slide 83

Slide 83 text

override func didSelectPost() { FoodBookApiClient.upload(image) { self.extensionContext!.completeRequestReturningItems([], completionHandler: nil) } }

Slide 84

Slide 84 text

Let's see it in action!

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Can also bring your own view controller

Slide 87

Slide 87 text

Action Extensions

Slide 88

Slide 88 text

Act upon content user is looking at

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Access to Entire DOM

Slide 91

Slide 91 text

Supply JavaScript that formats the input and handles the output

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Action.js var Action = function() {}; Action.prototype = { run: function(arguments) { }, finalize: function(arguments) { } }; var ExtensionPreprocessingJS = new Action

Slide 94

Slide 94 text

run: function(arguments) { arguments.completionFunction({ "host": window.location.host, "path": window.location.pathname }) },

Slide 95

Slide 95 text

App will receive PropertyList Item Type

Slide 96

Slide 96 text

func beginRequestWithExtensionContext(context: NSExtensionContext) { self.extensionContext = context var itemProvider: NSItemProvider? if let item = context.inputItems.first as? NSExtensionItem { if let attachment = item.attachments?.first as? NSItemProvider { itemProvider = attachment } } let itemType = String(kUTTypePropertyList) if itemProvider?.hasItemConformingToTypeIdentifier(itemType) == true { itemProvider?.loadItemForTypeIdentifier(itemType, options: nil) { item, error in let dictionary = item as NSDictionary NSOperationQueue.mainQueue().addOperationWithBlock { let preprocessingResults = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as [NSObject:AnyObject] self.itemLoadCompletedWithPreprocessingResults(preprocessingResults) } } } else { self.doneWithResults(nil) } }

Slide 97

Slide 97 text

let dictionary = item as NSDictionary NSOperationQueue.mainQueue().addOperationWithBlock { let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as NSDictionary self.itemLoadCompletedWithPreprocessingResults(results)

Slide 98

Slide 98 text

func itemLoadCompletedWithPreprocessingResults(results: NSDictionary) { let host = results["host"] as String let path = results["path"] as String let newURL = "http://cat.\(host).meowbify.com\(path)" doneWithResults(["newURL": newURL]) }

Slide 99

Slide 99 text

Prepare the results to go back to the JavaScript object

Slide 100

Slide 100 text

func doneWithResults(results: NSDictionary?) { if let finalizeResults = results { var resultsDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: finalizeResults] let itemType = kUTTypePropertyList as NSString var resultsProvider = NSItemProvider(item: resultsDictionary, typeIdentifier: itemType) var resultsItem = NSExtensionItem() resultsItem.attachments = [resultsProvider] extensionContext?.completeRequestReturningItems([resultsItem], completionHandler: nil) } else { extensionContext?.completeRequestReturningItems(nil, completionHandler: nil) } extensionContext = nil }

Slide 101

Slide 101 text

Handle the results in JavaScript

Slide 102

Slide 102 text

finalize: function(arguments) { window.location = arguments["newURL"]; }

Slide 103

Slide 103 text

OMG, can we run it yet?

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Questions?

Slide 106

Slide 106 text

Thank you! Slide links: http://speakerdeck.com/subdigital/ios8-app-extension Code links: https://github.com/subdigital/pragma-mark-2014 Ben Scheirman [email protected] @subdigital @nsscreencast