Slide 1

Slide 1 text

iOS Extension Prince

Slide 2

Slide 2 text

⺩王品⼠士 (Prince) @wpsteak

Slide 3

Slide 3 text

• App Extensions • Today Widget & Demo

Slide 4

Slide 4 text

2007

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

App

Slide 7

Slide 7 text

App Folder Badge

Slide 8

Slide 8 text

Scenario 1

Slide 9

Slide 9 text

Edit a photo Photo Home Find App Wait App Open Find Photo Edit Photo

Slide 10

Slide 10 text

Edit a photo Photo Select App Edit Photo Photo Editing Extension

Slide 11

Slide 11 text

Scenario 2

Slide 12

Slide 12 text

See scores from your favorite team Home Find App Wait App Open wait for loading Auto upload Rate me Cache Sync 8:1

Slide 13

Slide 13 text

See scores from your favorite team Home 8:1 Today Extension Widget

Slide 14

Slide 14 text

How an App Extension Works

Slide 15

Slide 15 text

Open URL Shared Resources App
 extension Host
 app Containing
 app Request Response

Slide 16

Slide 16 text

App
 extension Host
 app Containing
 app

Slide 17

Slide 17 text

Host app • An app that a user employs to choose an app extension is called a host app Host
 app Today.app Today Chrome.app Share/Action Dropbox.app Document Provider Photo.app Photo Editing

Slide 18

Slide 18 text

• an app that contains one or more extensions is called a containing app Containing app Containing
 app extension extension extension

Slide 19

Slide 19 text

• an app that contains one or more extensions is called a containing app Containing app

Slide 20

Slide 20 text

App extension • an app extension lets you extend custom functionality and content beyond App
 extension

Slide 21

Slide 21 text

App extension • an app extension lets you extend custom functionality and content beyond

Slide 22

Slide 22 text

Today Extension

Slide 23

Slide 23 text

Today Extension

Slide 24

Slide 24 text

Today Extension

Slide 25

Slide 25 text

Today Extension

Slide 26

Slide 26 text

Today Extension

Slide 27

Slide 27 text

Dropbox TodayView Today.app How an App Extension Communicates

Slide 28

Slide 28 text

Dropbox TodayView Today.app Database How an App Extension Communicates

Slide 29

Slide 29 text

Dropbox.app Dropbox TodayView Today.app Open URL Database How an App Extension Communicates

Slide 30

Slide 30 text

Open URL Shared Resources App
 extension Host
 app Containing
 app How an App Extension Communicates

Slide 31

Slide 31 text

Share Extension

Slide 32

Slide 32 text

Share Extension

Slide 33

Slide 33 text

Share Extension

Slide 34

Slide 34 text

Share Extension

Slide 35

Slide 35 text

Share Extension

Slide 36

Slide 36 text

App
 extension Host
 app Containing
 app Pocket AddToPocket Chrome Server

Slide 37

Slide 37 text

App
 extension Host
 app AddToPocket Chrome

Slide 38

Slide 38 text

Accessed via Apple frameworks code App
 extension Host
 app System Framework

Slide 39

Slide 39 text

App
 extension Host
 app AddToPocket Chrome UIActivityViewController SLComposeServiceViewController Accessed via Apple frameworks code

Slide 40

Slide 40 text

Extension Type Host App host app use Today Today.app NotificationCenter.framework Share any app UIActivityViewController Action any app UIActivityViewController Photo Editing Photo.app PhotosUI.framework
 Photos.framework Document Provider any app UIDocumentPickerViewController Custom Keyboard any app Host
 app

Slide 41

Slide 41 text

Extension Type Enter point Today UIViewController Share SLComposeServiceViewController Action SLComposeServiceViewController UI and non-UI variants Photo Editing UIViewController Document Provider UIDocumentPickerViewController UI and non-UI variants Custom Keyboard UIInputViewController App
 extension

Slide 42

Slide 42 text

App
 extension Host
 app Containing
 app Pocket AddToPocket Chrome Server

Slide 43

Slide 43 text

App
 extension Containing
 app Pocket AddToPocket Different process • Isolated address space • Executes independently • System optimizes separately Run Separately

Slide 44

Slide 44 text

App
 extension Containing
 app Shared Resources Share Resource App Group • UserDefault • FileManager

Slide 45

Slide 45 text

Share Resource NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:@“group.domain"]; NSURL *directory = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.domain"];

Slide 46

Slide 46 text

App
 extension Containing
 app Pocket AddToPocket Server Server • Same data model • Same logic • Same views Share Code

Slide 47

Slide 47 text

App
 extension Containing
 app Pocket AddToPocket Server Server • Framework • Static library Share Code

Slide 48

Slide 48 text

Share Code NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.domain"]; NSString *sorPath = [NSString stringWithFormat:@"%@/Dylib.framework",[[NSBundle mainBundle] bundlePath]]; NSString *desPath = [NSString stringWithFormat:@“%@/Library/Caches/ Dylib.framework",containerURL.path]; BOOL copyResult = [[NSFileManager defaultManager] copyItemAtPath: sorPath toPath:desPath error:&err];

Slide 49

Slide 49 text

• Containing app is still most important • Extension cannot without containing app • Don't have full use of system resources • Make it seamless • Make it useful

Slide 50

Slide 50 text

Demo

Slide 51

Slide 51 text

Demo • a light bulb • sample code • Wifi Environment

Slide 52

Slide 52 text

App Extension • Create an app extension by adding a new target to an app • Add necessary libraries to extension target • ViewController

Slide 53

Slide 53 text

Load View • with storyboard • NSExtensionMainStoryboard • without interface file • NSExtensionPrincipalClass

Slide 54

Slide 54 text

viewController’s life cycle • initWithCoder: • viewDidLoad • widgetMarginInsetsForProposedMarginInsets: • widgetPerformUpdateWithCompletionHandler: • viewWillAppear: • viewDidAppear:

Slide 55

Slide 55 text

• widgetMarginInsetsForProposedMarginInsets

Slide 56

Slide 56 text

User Interface • UIView • UIButton • UILabel • UITextField • UIScrollView

Slide 57

Slide 57 text

Some APIs are Unavailable to App Extensions • Access a sharedApplication object • Use any API marked in header files with the NS_EXTENSION_UNAVAILABLE macro, or similar unavailability macro, or any API in an unavailable framework • Access the camera or microphone on an iOS device • Perform long-running background tasks • The specifics of this limitation vary by platform, as described in the extension point chapters in this document. • (An app extension can initiate uploads or downloads using an NSURLSession object, with results of those operations reported to the containing app.) • Receive data using AirDrop • (An app extension can send data using AirDrop in the same way an app does: by employing the UIActivityViewController class.)

Slide 58

Slide 58 text

App Store • NOTE ABOUT 64-BIT ARCHITECTURE • An app extension target must include the arm64 (iOS) or x86_64 architecture (OS X) in its Architectures build settings or it will be rejected by the App Store. Xcode includes the appropriate 64-bit architecture with its “Standard architectures” setting when you create a new app extension target. • If your containing app target links to an embedded framework, the app must also include 64-bit architecture or it will be rejected by the App Store.

Slide 59

Slide 59 text

App Store

Slide 60

Slide 60 text

End