菜鸟教程小白 发表于 2022-12-13 11:39:42

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: = [&#34;text&#34;:&#34;test&#34;, &#34;badgeValue&#34;: 100 ]

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

}
</code></pre>

<p>在 AppDelegate 中:(当 iOS 应用未运行/不在前台时未收到)</p>

<pre><code>func session(session: WCSession, didReceiveMessage message: , replyHandler: () -&gt; Void) {
    let text = message[&#34;text&#34;] as! String
    let badgeValue = message[&#34;badgeValue&#34;] as! Int

    dispatch_async(dispatch_get_main_queue()) { () -&gt; Void in

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

    }

}
</code></pre>

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

<pre><code>func requestedUpdateDidBegin(){

      dispatch_async(dispatch_get_main_queue()) { () -&gt; 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&#39;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]
查看完整版本: ios - 如何使用来自complicationController的sendMessage唤醒iOS父应用程序