如何使用complicationController的sendMessage唤醒iOS父应用 [英] how to wake up iOS parent app with sendMessage from complicationController

查看:137
本文介绍了如何使用complicationController的sendMessage唤醒iOS父应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图通过发送来自watchkit扩展程序的消息来唤醒iOS父级应用.

I am trying to wake up the iOS parent app by sending a message from watchkit extension.

这仅在从watchApp/ViewController调用以下sendMessage函数时才起作用.从ComplicationController调用时,将发送消息,但iOS父应用程序现在确实会唤醒.

This though does only work when below sendMessage function is called from the watchApp / ViewController. When it is called from ComplicationController, the message is sent, but the iOS parent app does now wake up.

任何建议都值得赞赏. (请在Swift中引用任何代码)

Any advice appreciated. (please any code reference in Swift)

以下是简化代码:

在AppDelegate和ExtensionDelegate中:

In AppDelegate and ExtensionDelegate:

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

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

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

In ExtensionDelegate: (no problem here, message is successfully sent)

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应用未运行/不在前台时不会收到

In AppDelegate: (not received when iOS app not running / not in foreground)

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(确实发送消息但不唤醒父应用程序)中调用该函数的方式:

this is how the function is called from Complication Controller (which does send the message but not awake the parent app):

  func requestedUpdateDidBegin(){

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

            let extensionDelegate = ExtensionDelegate()
            extensionDelegate.loadData()

        }
    }

推荐答案

主要问题是您试图包含(嵌套)您的并发症数据源中的异步调用.但是,您请求的更新将达到其方法的结尾,并且实际上不会进行时间表更新(因为您没有重新加载或延长时间轴,即使您有时间,也不会及时收到用于当前更新的新数据.

The main problem is that you're trying to include (nested) asynchronous calls within your complication data source. However, your requested update will have reached the end of its method, and no timeline update will actually take place (since you didn't reload or extend the timeline, and even if you had, no new data would have been received in time for the current update).

由于没有新数据可用于计划的更新,因此您必须执行第二次更新,才能使用一次新数据.不仅不需要执行两次连续更新,而且还会浪费更多的日常并发症预算.

Since no new data would be available for the scheduled update, you'd have to perform a second update to use the new data once it was received. Performing two back-to-back updates is not only unnecessary, it wastes more of your daily complication budget.

Apple建议您

Apple recommends that you fetch and cache the data in advance of the update, so the complication data source can directly return the requested data to the complication server.

数据源类的工作是尽快向ClockKit提供任何请求的数据.数据源方法的实现应尽量少.请勿使用数据源方法从网络中获取数据,计算值或执行任何可能会延迟该数据传递的操作.如果您需要获取或计算并发症数据,请在iOS应用或WatchKit扩展的其他部分进行处理,然后将数据缓存在并发症数据源可以访问的位置.数据源方法唯一要做的就是获取缓存的数据,并将其设置为ClockKit要求的格式.

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.

如何更新并发症?

  • 使用手机的后台更新来传输要处理的数据,以便进行并发症的下一次计划更新. transferUserInfoupdateApplicationContext适用于这种类型的更新.

  • Use background updates from the phone to transfer the data to be on hand for the complication's next scheduled update. transferUserInfo and updateApplicationContext are suited for this type of update.

使用transferCurrentComplicationUserInfo立即传输并发症数据并更新您的时间轴.

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

Both of these approaches have the advantage of only needing one update to occur.

这篇关于如何使用complicationController的sendMessage唤醒iOS父应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆