ios - 如何使用来自complicationController的sendMessage唤醒iOS父应用程序
<p><p>我正在尝试通过从 watchkit 扩展发送消息来唤醒 iOS 父应用。 </p>
<p>这只有在从 watchApp/ViewController 调用下面的 sendMessage 函数时才有效。当从 ComplicationController 调用它时,会发送消息,但 iOS 父应用程序现在确实会唤醒。 </p>
<p>任何建议表示赞赏。 (请在 Swift 中引用任何代码)</p>
<p>这里是简化代码:</p>
<p>在 AppDelegate 和 ExtensionDelegate 中:</p>
<pre><code>override init() {
super.init()
setupWatchConnectivity()
}
private func setupWatchConnectivity() {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
</code></pre>
<p>在ExtensionDelegate中:(这里没问题,消息发送成功)</p>
<pre><code>func sendMessage(){
let session = WCSession.defaultSession()
let applicationData: = ["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")
}
</code></pre>
<p>在 AppDelegate 中:(当 iOS 应用未运行/不在前台时未收到)</p>
<pre><code>func session(session: WCSession, didReceiveMessage message: , replyHandler: () -> 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 )
}
}
</code></pre>
<p>这是从复杂 Controller 调用函数的方式(它确实发送消息但不唤醒父应用程序):</p>
<pre><code>func requestedUpdateDidBegin(){
dispatch_async(dispatch_get_main_queue()) { () -> Void in
let extensionDelegate = ExtensionDelegate()
extensionDelegate.loadData()
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>主要问题是您试图包含(嵌套)<a href="https://stackoverflow.com/a/36857535/4151918" rel="noreferrer noopener nofollow">asynchronous calls within your complication data source</a> .但是,您请求的更新将到达其方法的末尾,实际上不会发生时间线更新(自 <a href="https://stackoverflow.com/a/34235042/4151918" rel="noreferrer noopener nofollow">you didn't reload or extend the timeline</a> 以来,即使您有,也不会及时收到当前更新的新数据)。</p>
<p><em>由于没有可用于预定更新的新数据,您必须执行<strong>第二次</strong>更新才能使用新数据<strong>一旦</strong>收到.执行两次背靠背更新不仅没有必要,而且会浪费更多的日常并发症预算。</em></p>
<p>Apple 建议您使用 <a href="https://developer.apple.com/library/watchos/documentation/General/Conceptual/AppleWatch2TransitionGuide/CreatingaComplication.html#//apple_ref/doc/uid/TP40015234-CH10-SW1" rel="noreferrer noopener nofollow">fetch and cache the data <em>in advance</em> of the update</a> ,所以并发症数据源可以直接将请求的数据返回给并发症服务器。</p>
<blockquote>
<p>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.</p>
</blockquote>
<p><strong>如何更新并发症?</strong></p>
<ul>
<li><p>使用手机的后台更新来传输手头的数据,以便并发症的下一次计划更新。 <code>transferUserInfo</code> 和 <code>updateApplicationContext</code> 适合这种类型的更新。</p></li>
<li><p>使用 <code>transferCurrentComplicationUserInfo</code> 到 <a href="https://stackoverflow.com/a/34755373/4151918" rel="noreferrer noopener nofollow">immediately transfer complication data and update your timeline</a> .</p></li>
</ul>
<p>这两种方法的优点是只需要<em>一次</em>更新。</p></p>
<p style="font-size: 20px;">关于ios - 如何使用来自complicationController的sendMessage唤醒iOS父应用程序,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33533447/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33533447/
</a>
</p>
页:
[1]