Example Code: Sending Data From Apple Watch To iPhone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for Xcode 8.3 Swift Update