OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使用来自complicationController的sendMessage唤醒iOS父应用程序 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:39
标题: ios - 如何使用来自complicationController的sendMessage唤醒iOS父应用程序

我正在尝试通过从 watchkit 扩展发送消息来唤醒 iOS 父应用。

这只有在从 watchApp/ViewController 调用下面的 sendMessage 函数时才有效。当从 ComplicationController 调用它时,会发送消息,但 iOS 父应用程序现在确实会唤醒。

任何建议表示赞赏。 (请在 Swift 中引用任何代码)

这里是简化代码:

在 AppDelegate 和 ExtensionDelegate 中:

override init() {
    super.init()
    setupWatchConnectivity()
}

private func setupWatchConnectivity() {
    if WCSession.isSupported() {
        let session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}

在ExtensionDelegate中这里没问题,消息发送成功)

func sendMessage(){
        let session = WCSession.defaultSession()
        let applicationData:[String:AnyObject] = ["text":"test", "badgeValue": 100 ]

        session.sendMessage(applicationData, replyHandler: {replyMessage in
            print("reply received from iphone")
            }, errorHandler: {(error ) -> Void in
                // catch any errors here
                print("no reply message from phone")
        })
    }
    print("watch sent message")

}

在 AppDelegate 中当 iOS 应用未运行/不在前台时未收到)

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    let text = message["text"] as! String
    let badgeValue = message["badgeValue"] as! Int

    dispatch_async(dispatch_get_main_queue()) { () -> Void in

        print("iphone received message from watch App")
        self.sendNotification(text, badgeValue: badgeValue)
        let applicationDict = ["wake": "nowAwake"]
        replyHandler(applicationDict as [String : String])

    }

}

这是从复杂 Controller 调用函数的方式(它确实发送消息但不唤醒父应用程序):

  func requestedUpdateDidBegin(){

        dispatch_async(dispatch_get_main_queue()) { () -> Void in

            let extensionDelegate = ExtensionDelegate()
            extensionDelegate.loadData()

        }
    }



Best Answer-推荐答案


主要问题是您试图包含(嵌套)asynchronous calls within your complication data source .但是,您请求的更新将到达其方法的末尾,实际上不会发生时间线更新(自 you didn't reload or extend the timeline 以来,即使您有,也不会及时收到当前更新的新数据)。

由于没有可用于预定更新的新数据,您必须执行第二次更新才能使用新数据一旦收到.执行两次背靠背更新不仅没有必要,而且会浪费更多的日常并发症预算。

Apple 建议您使用 fetch and cache the data in advance of the update ,所以并发症数据源可以直接将请求的数据返回给并发症服务器。

The job of your data source class is to provide ClockKit with any requested data as quickly as possible. The implementations of your data source methods should be minimal. Do not use your data source methods to fetch data from the network, compute values, or do anything that might delay the delivery of that data. If you need to fetch or compute the data for your complication, do it in your iOS app or in other parts of your WatchKit extension, and cache the data in a place where your complication data source can access it. The only thing your data source methods should do is take the cached data and put it into the format that ClockKit requires.

如何更新并发症?

这两种方法的优点是只需要一次更新。

关于ios - 如何使用来自complicationController的sendMessage唤醒iOS父应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33533447/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4