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

how to wake up iOS parent app with sendMessage from complicationController

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

        }
    }

主要问题是您试图包含(嵌套). However, your requested update will have reached the end of its method, and no timeline update will actually take place (since ,即使您这样做了,也不会及时收到当前更新的新数据。

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

Apple推荐您fetch and cache the data in advance of the update,这样复杂数据源可以直接return将请求的数据发送到复杂服务器。

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.

如何更新并发症?

  • 使用来自 phone 的后台更新来传输手头的数据,以供并发症的下一次计划更新使用。 transferUserInfoupdateApplicationContext 适合这种类型的更新。

  • 使用transferCurrentComplicationUserInfo

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