我想通過從watchkit擴展發送消息來喚醒iOS父應用程序。如何喚醒從complicationController sendMessage的iOS父應用程序
這雖然只在watchApp/ViewController中調用sendMessage函數時才起作用。當它從ComplicationController中調用時,會發送消息,但iOS父應用程序現在會喚醒。
任何意見讚賞。 (請在夫特任何代碼基準)
在這裏,簡化的代碼:
在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])
}
}
這是怎樣的功能是從併發症控制器稱爲(它發送消息,但還沒醒父應用程序):
func requestedUpdateDidBegin(){
dispatch_async(dispatch_get_main_queue()) {() -> Void in
let extensionDelegate = ExtensionDelegate()
extensionDelegate.loadData()
}
}
我剛剛在模擬器和iPhone /手錶上測試了您的代碼。即使我從未打開它,它也會喚醒應用程序。它適用於應用程序運行時,應用程序在後臺並且應用程序完全被終止的情況。 – joern
是什麼讓你覺得應用程序沒有被喚醒? – joern
謝謝Joern的努力。非常感謝。只用上面的代碼進行測試,你是正確的。真奇怪。看起來問題不在於上述代碼,而在於我如何稱呼它。在我的應用程序中,我從ComplicationController調用sendMessage(它位於ExtensionDelegate中),在這種情況下會發送消息,但父應用程序不會喚醒。如果我改爲從ViewController觸發應用程序中的sendMessage,則會發送消息,並且父應用程序確實會喚醒。 (我怎麼知道是通過回覆收到或不收看) – TPeter