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

Practical WatchKit

Practical WatchKit

Given at NSLondon on 23/04/2015.

Neil Kimmett

April 23, 2015
Tweet

More Decks by Neil Kimmett

Other Decks in Technology

Transcript

  1. class WKInterfaceButton : WKInterfaceObject { func setTitle(title: String?) func setAttributedTitle(attributedTitle:

    NSAttributedString?) func setBackgroundColor(color: UIColor?) func setBackgroundImage(image: UIImage?) func setBackgroundImageData(imageData: NSData?) func setBackgroundImageNamed(imageName: String?) func setEnabled(enabled: Bool) }
  2. NSUSERDEFAULTS let group = "group.me.kimmett.pushthebutton" let defaults = NSUserDefaults(suiteName: group)

    defaults?.setInteger(42, forKey: "IMPORTANT_NUMBER") defaults?.integerForKey("IMPORTANT_NUMBER") // 42
  3. NSFILEMANAGER let group = "group.me.kimmett.pushthebutton" let manager = NSFileManager.defaultManager() if

    let dir = manager.containerURLForSecurityApplicationGroupIdentifier(group) { // do something with dir }
  4. CORE DATA if let dir = manager.containerURLForSecurityApplicationGroupIdentifier(group) { let url

    = dir.URLByAppendingPathComponent(“Database.sqlite”) let coordinator = … coordinator.migratePersistentStore(store, toURL: url, options: nil, withType: NSSQLiteStoreType, error: &error) }
  5. “on Yosemite and iOS 8, the method discards many-to-many relationships”

    — http://mjtsai.com/blog/2014/11/22/core-data-relationships-data- loss-bug/
  6. WKINTERFACECONTROLLER class func openParentApplication( userInfo: [NSObject : AnyObject], reply: (([NSObject

    : AnyObject]!, NSError!) -> Void)?) -> Bool UIAPPLICATIONDELEGATE func application(application: UIApplication, handleWatchKitExtensionRequest: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!)
  7. OPENPARENTAPPLICATION var task: UIBackgroundTaskIdentifier task = application.beginBackgroundTaskWithExpirationHandler { reply([:]) application.endBackgroundTask(task)

    task = UIBackgroundTaskInvalid } // do something with userInfo reply(…) application.endBackgroundTask(task) task = UIBackgroundTaskInvalid
  8. ONE WEIRD TRICK... “…you have to begin – and end,

    after two seconds – an empty background task right at the beginning of the delegate method.” — http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird- trick-to-fix-openparentapplicationreply
  9. ONE WEIRD TRICK... var bogusTask: UIBackgroundTaskIdentifier bogusTask = application.beginBackgroundTaskWithExpirationHandler {

    application.endBackgroundTask(bogusTask) } let time = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC))) dispatch_after(time, dispatch_get_main_queue(), { UIApplication.sharedApplication().endBackgroundTask(bogusTask) }) // do your actual work
  10. WKINTERFACECONTROLLER func updateUserActivity(type: String, userInfo: [NSObject : AnyObject]?, webpageURL: NSURL?)

    UIAPPLICATIONDELEGATE func application(application: UIApplication, continueUserActivity: NSUserActivity, restorationHandler: ([AnyObject]!) -> Void) -> Bool
  11. REFERENCES ▸ “Core Data Relationships Data Loss Bug” by Michael

    Tsai http://mjtsai.com/blog/2014/11/22/core-data-relationships-data- loss-bug/ ▸ “Tap Distance” by David Smith http://david-smith.org/blog/2015/02/03/ailw-tap-distance/
  12. REFERENCES ▸ “One weird trick to “fix” openParentApplication:reply:” by Brian

    Gilham http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird- trick-to-fix-openparentapplicationreply