Skip to content

Instantly share code, notes, and snippets.

@GFJHogue
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GFJHogue/d6d8a077eba1b5011e62 to your computer and use it in GitHub Desktop.
Save GFJHogue/d6d8a077eba1b5011e62 to your computer and use it in GitHub Desktop.
Example Code: Sending Data From Apple Watch To iPhone
// WatchToPhoneDemo/WatchToPhoneDemo WatchKit Extension/InterfaceController.swift
class InterfaceController: WKInterfaceController {
var str: String = "Hello iPhone!"
@IBAction func button() {
let dict: Dictionary = ["message": str]
WKInterfaceController.openParentApplication(dict, reply: {(reply, error) -> Void in
println("Reply recieve from iPhone app.")
})
}
}
// WatchToPhoneDemo/WatchToPhoneDemo/AppDelegate.swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
NSNotificationCenter.defaultCenter().postNotificationName("WatchKitReq", object: userInfo)
}
}
// WatchToPhoneDemo/WatchToPhoneDemo/ViewController.swift
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("handleWatchKitNotification:"), name: "WatchKitReq", object: nil)
}
func handleWatchKitNotification(notification: NSNotification) {
if let userInfo = notification.object as? [String : String] {
label.text = userInfo["message"]
}
}
}
@GFJHogue
Copy link
Author

Updated for Xcode 8.3 Swift Update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment