简体   繁体   English

如何从父级ios应用程序调用watchkit扩展中的方法

[英]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.

相关问题 具有WatchKit扩展名的父级应用程序是否为iOS 8及更高版本? - Does parent app with WatchKit extension be iOS 8 and later? WatchKit扩展:如何从主机iOS应用读取数据? - WatchKit extension: how to read data from host iOS app? 通过WatchKit扩展检测iOS应用是否在前台 - Detect if iOS app is in foreground from WatchKit extension Watchkit,如何从父应用程序中的视图控制器调用函数 - Watchkit, how to call a function from a view controller in the parent app 如何从Swift的Today扩展中的父iPhone应用程序中调用方法? - How to call a method in the parent iPhone app from Today Extension in Swift? 如何从iOS应用显示Watchkit概览 - How display watchkit glance from iOS app iOS WatchKit - 如何确定您的代码是在手表扩展程序还是应用程序中运行 - iOS WatchKit - how to determine if your code is running in watch extension or the app 如何通过存储“回复”块在iOS应用和WatchKit扩展之间进行通信? - How to communicate between an iOS app and the WatchKit extension by storing the “reply” block? 当WatchKit App / Extension唤醒时,我可以在父iOS应用程序中调试代码吗? - Can I debug code in the parent iOS App when it is awoken by a WatchKit App/Extension? 如何从WatchKit Extension中调用CoreData代理框架? - How do I call the CoreData proxy framework from WatchKit Extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM