[英]How to call the method in watchkit extension from parent ios app
We can call the method openParentApplication:reply:
in parent ios app from watch kit extension. 我们可以从手表套件扩展中的父ios应用程序中调用方法
openParentApplication:reply:
But is there any way to call the method in watchkit extension from parent ios app ? 但是有什么方法可以从父ios应用程序调用watchkit扩展中的方法吗?
For example:In my app when user add event in ios app then watchkit event list also should refresh so for that i need to call the refresh method in watchkit extension when user add new event in main app. 例如:在我的应用程序中,当用户在ios应用程序中添加事件时,监视包事件列表也应刷新,因此当用户在主应用程序中添加新事件时,我需要在监视包扩展中调用refresh方法。
Please help. 请帮忙。
Thanks. 谢谢。
您不能直接从watchkit扩展中调用方法,但是可以发送darwin通知(或使用MMWormhole
库( 在此处 ),并在接收到该方法后执行适当的方法。
You can use the built-in WatchConnectivity framework to send message from iOS app to the paired Apple Watch. 您可以使用内置的WatchConnectivity框架将消息从iOS应用发送到配对的Apple Watch。
1) First, activate watch connectivity session both in iOS app and WatchKit extension. 1)首先,激活手表连接无论是在iOS应用和WatchKit扩展会话。 On the iOS side it can be done in
application didFinishLaunchingWithOptions
of the app delegate. 在iOS端,可以在
application didFinishLaunchingWithOptions
程序委托的应用application didFinishLaunchingWithOptions
中完成。 On watch side you can run this code in applicationDidFinishLaunching
method of the WatchKit extension delegate. 在监视方面,您可以在WatchKit扩展委托的
applicationDidFinishLaunching
方法中运行此代码。
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
2) Now send a message from your iOS app. 2)现在从您的iOS应用发送消息。
let session = WCSession.defaultSession()
session.sendMessage(["message from iOS app":"🐥"], replyHandler: { reply in
// Handle reply from watch (optional)
}, errorHandler: nil)
3) Receive the message in your WatchKit extension by implementing the session didReceiveMessage
method in your WCSessionDelegate
delegate class. 3)通过在
WCSessionDelegate
委托类中实现session didReceiveMessage
WCSessionDelegate
方法,在WatchKit扩展中接收消息。
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
if let message = message["message from iOS app"] {
NSNotificationCenter.defaultCenter().postNotificationName("myapp.reload", object: self, userInfo: ["data": message])
}
}
Upon receiving the message from iOS we are sending a notification with postNotificationName
method. 从iOS收到消息后,我们将使用
postNotificationName
方法发送通知。
4) Subscribe to this notification in your InterfaceController that needs updating (or anywhere else where you want to receive this update notification). 4)在需要更新的InterfaceController中(或您要接收此更新通知的其他任何地方)订阅此通知。
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didReceiveReloadNotification:", name: "myapp.reload", object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self,
name: "myapp.reload", object: nil)
}
5) Finally, implement the notification handler method. 5)最后,实现通知处理程序方法。 This is where you can update your UI.
您可以在此处更新UI。
func didReceiveReloadNotification(notification: NSNotification) {
let userInfo = notification.userInfo as? [String: String]
if let userInfo = userInfo, data = userInfo["data"] {
// Update UI
}
}
Note: for the sake of readability I am using inline text string for the notification name "myapp.reload" and the message key "message from iOS app". 注意:为了便于阅读,我将内联文本字符串用作通知名称“ myapp.reload”和消息键“ iOS应用程序的消息”。 But in the real app it is better to use properties for these text strings to avoid typos.
但是,在实际应用中,最好对这些文本字符串使用属性,以避免输入错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.