Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Making Apple Devices Work Together

Making Apple Devices Work Together

Most of us have more than one iOS device. Whether it’s an iPhone and iPad, iPhone and Apple Watch, or iPad and MacBook, these devices no longer live in isolation. Bound together by iCloud and your Apple ID, these devices are more powerful when they work together. This session will cover making them communicate using new and old APIs alike: Handoff and iCloud. We’ll cover the best way to share files and preferences among all of your users’ devices. If you don’t have a Mac app but do have a website, we’ll cover transferring control back and forth using Handoff, as well as sharing passwords with iCloud Keychain. By the end, this session will have you multitasking like a pro.

Jeff Kelley

June 11, 2015
Tweet

More Decks by Jeff Kelley

Other Decks in Programming

Transcript

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

    View full-size slide

  2. 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

    View full-size slide

  3. 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

    View full-size slide

  4. 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

    View full-size slide

  5. 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

    View full-size slide

  6. 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

    View full-size slide

  7. 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

    View full-size slide

  8. 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

    View full-size slide

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

    View full-size slide

  10. 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)
    }

    View full-size slide

  11. 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
    }

    View full-size slide

  12. 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

    View full-size slide

  13. 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

    View full-size slide

  14. 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

    View full-size slide

  15. 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

    View full-size slide