gpt4 book ai didi

ios - 如何使用来自 complicationController 的 sendMessage 唤醒 iOS 父应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:33 24 4
gpt4 key购买 nike

我正在尝试通过从 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])

}

}

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

  func requestedUpdateDidBegin(){

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

let extensionDelegate = ExtensionDelegate()
extensionDelegate.loadData()

}
}

最佳答案

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

由于没有新数据可用于计划的更新,您必须执行第二次更新以在收到后使用新数据.连续执行两次更新不仅没有必要,而且会浪费您更多的日常并发症预算。

苹果推荐你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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com