Slide 1

Slide 1 text

Making Apple Devices Work Together Jeff Kelley (@SlaunchaMan) AltConf, June 11th, 2015

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

What problems are we solving? • Synchronizing data between devices • Preventing duplicate effort on the user’s part • Seamlessly transitioning state from one device to the next • Integrating a mobile app with a desktop website

Slide 4

Slide 4 text

Synchronizing Data Between Devices • Not a problem we’re going to solve in this 30 minutes • Tons of solutions out there, both Apple’s and third- party solutions • CloudKit for syncing data using iCloud • With iOS 9, includes JS web service API! • Parse et al. for syncing data with other platforms, too

Slide 5

Slide 5 text

Synchronizing Data Between Devices • Other kinds of data you want to keep in sync • Passwords • iCloud Keychain • Articles to read later • Reading List • Simple data • iCloud Key-Value storage

Slide 6

Slide 6 text

Implementing iCloud Keychain • Store usernames and passwords just as you normally would in the keychain • Square Valet • FXKeychain • Add a new attribute to keychain items: kSecAttrSynchronizable • VALSynchronizableValet

Slide 7

Slide 7 text

Implementing iCloud Key- Value Storage • Add the iCloud Key-Value Store value to your Entitlements.plist • Use NSUbiquitousKeyValueStore like NSUserDefaults • But also keep NSUserDefaults around in case the user signs out of iCloud • Listen for change notifications and update local state

Slide 8

Slide 8 text

Demo

Slide 9

Slide 9 text

Seamlessly Transitioning State Between Devices • When a user starts doing something on one device, they may want to finish on another • Entering long-form text on iPhone instead of Apple Watch • Continuity allows users to use all of their devices together • Handoff is a feature of Continuity that your app can implement

Slide 10

Slide 10 text

Implementing Handoff • NSUserActivity • Describe what the user is doing • Describe the document the user has open with UIDocument or NSDocument • Automatically picked up via Bluetooth on eligible devices

Slide 11

Slide 11 text

Implementing Handoff let userActivity = NSUserActivity( activityType: "com.slaunchaman.PhotoSharer.viewingPhoto") userActivity.title = "Viewing Image" self.userActivity = userActivity self.userActivity?.becomeCurrent()

Slide 12

Slide 12 text

Implementing Handoff override func updateUserActivityState(activity: NSUserActivity) { let resizedImage = selectedImage? .resizedImageToFitInSize(CGSize(width: 250, height: 250), scaleIfSmaller: false) let representation = UIImageJPEGRepresentation(resizedImage, 0.5) activity.addUserInfoEntriesFromDictionary(["Image": representation]) super.updateUserActivityState(activity) }

Slide 13

Slide 13 text

Implementing Handoff func application(application: UIApplication, willContinueUserActivityWithType userActivityType: String) -> Bool { return true } func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]!) -> Void) -> Bool { if let navigationController = window?.rootViewController as? UINavigationController, rootViewController = navigationController.viewControllers.first as? ViewController, imageData = userActivity.userInfo?["Image"] as? NSData, image = UIImage(data: imageData) { rootViewController.selectedImage = image } return true }

Slide 14

Slide 14 text

Demo

Slide 15

Slide 15 text

Other Handoff Uses • Apple Watch uses user activities when the user opens your app from: • Glance • ClockKit Complication • If your watch app needs you to open your iPhone app (e.g. to log in to a web service), use Handoff to open it directly onto the login screen • Send documents around between devices • Automatically managed for document-based apps • NSUserActivity is the basis for new app indexing and search

Slide 16

Slide 16 text

Integrating Your Website • Shared Web Credentials • Add the domain(s) with which you’re sharing credentials to the com.apple.developer.associated‑domains entitlement • Add an apple-app-site-association file to the domain(s) • CMS signed by a TLS certificate, HTTPS, no redirects • Validated at install time • Use SecRequestSharedWebCredential() and SecAddSharedWebCredential() to manage password data • Use SecCreateSharedWebCredentialPassword() to suggest passwords like Safari

Slide 17

Slide 17 text

Integrating Your Website • iOS App to Website • Set the webpageURL property of your NSUserActivity • The default browser opens the URL if no app claims the user activity type • Website to iOS App • Add the domain(s) with which you’re handing off data to the com.apple.developer.associated‑domains entitlement • Add an apple-app-site-association file to the domain(s) • Respond to NSUserActivityTypeBrowsingWeb

Slide 18

Slide 18 text

For More Info • https://developer.apple.com/handoff/ • https://developer.apple.com/library/ios/ documentation/Security/Reference/ SharedWebCredentialsRef/ • http://www.appcoda.com/handoff-continuation- streams/ • https://github.com/square/Valet