Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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"]
}
}
}
Owner

GFJHogue commented Apr 14, 2015

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