How to send data from iphone to watchkit in swift

て烟熏妆下的殇ゞ 提交于 2019-12-17 19:57:19

问题


I am doing a sample project in which i like to send data from iphone to WatchKit. I have no idea of how to do this. Any help will be appreciated. Thanks in advance


回答1:


Add this method in the AppDelegate:

func application(application: UIApplication!,
handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
   reply: (([NSObject : AnyObject]!) -> Void)!)
{
   //define your dictionary
   reply(yourDict)
}

Add this anywhere in the Apple Watch Extension:

var dict = ["test" : 4] //your dictionary/request to sent to the iPhone
if !WKInterfaceController.openParentApplication(dict, reply: { (reply,error) -> Void in
    println("\(reply)") //your reply data as Dictionary
}) {
    println("ERROR")
}

The first function will trigger with WKInterfaceController.openParentApplication and will reply the dictionary in the reply parameter.

Developer forum: https://devforums.apple.com/message/1082689#1082689



来源:https://stackoverflow.com/questions/28762906/how-to-send-data-from-iphone-to-watchkit-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!