如何从 Watch on iPhone 触发后台进程(触发:Watch)?
How trigger background process from Watch on iPhone (trigger: Watch)?
我想向我的 Watch 应用程序添加向 iPhone 应用程序发送本地通知的功能(当 iPhone 应用程序在后台运行或 iPhone 被锁定时)。
我知道如何自己创建本地通知。
我要问的是如何通过(例如)点击 Apple Watch 上的按钮在 iPhone 上触发后台进程(其中还包含本地通知)。
WKInterfaceController.openParentApplication
是与 iPhone 交流的官方方式。 Documentation.
您在 userInfo
字典中传递参数并通过 reply
块检索结果。
在 iPhone 上,请求由 appDelegate 的 handleWatchKitExtensionRequest
方法处理。 Documentation
我的代码 InterfaceController.swift:
@IBAction func btn() {
sendMessageToParentApp("Button tapped")
}
// METHODS #2:
func sendMessageToParentApp (input:String) {
let dictionary = ["message":input]
WKInterfaceController.openParentApplication(dictionary, reply: { (replyDictionary, error) -> Void in
if let castedResponseDictionary = replyDictionary as? [String:String], responseMessage = castedResponseDictionary["message"] {
println(responseMessage)
self.lbl.setText(responseMessage)
}
})
}
接下来我在 AppDelegate.swift 中创建了新方法:
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
if let infoDictionary = userInfo as? [String:String], message = infoDictionary["message"] {
let response = "iPhone has seen this message." // odešle se string obsahující message (tedy ten String)
let responseDictionary = ["message":response] // tohle zase vyrobí slovník "message":String
NSNotificationCenter.defaultCenter().postNotificationName(notificationWatch, object: nil)
reply(responseDictionary)
}
}
如您所见,我使用通知让 iOS 应用程序知道按钮已被点击。在 ViewController.swift 中,我有 Notification Observer 和函数,每次观察者捕捉到用户点击手表按钮的通知时都会执行("notificationWatch" 是带有通知键的全局变量)。希望这对任何人都有帮助。
我想向我的 Watch 应用程序添加向 iPhone 应用程序发送本地通知的功能(当 iPhone 应用程序在后台运行或 iPhone 被锁定时)。
我知道如何自己创建本地通知。
我要问的是如何通过(例如)点击 Apple Watch 上的按钮在 iPhone 上触发后台进程(其中还包含本地通知)。
WKInterfaceController.openParentApplication
是与 iPhone 交流的官方方式。 Documentation.
您在 userInfo
字典中传递参数并通过 reply
块检索结果。
在 iPhone 上,请求由 appDelegate 的 handleWatchKitExtensionRequest
方法处理。 Documentation
我的代码 InterfaceController.swift:
@IBAction func btn() {
sendMessageToParentApp("Button tapped")
}
// METHODS #2:
func sendMessageToParentApp (input:String) {
let dictionary = ["message":input]
WKInterfaceController.openParentApplication(dictionary, reply: { (replyDictionary, error) -> Void in
if let castedResponseDictionary = replyDictionary as? [String:String], responseMessage = castedResponseDictionary["message"] {
println(responseMessage)
self.lbl.setText(responseMessage)
}
})
}
接下来我在 AppDelegate.swift 中创建了新方法:
func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {
if let infoDictionary = userInfo as? [String:String], message = infoDictionary["message"] {
let response = "iPhone has seen this message." // odešle se string obsahující message (tedy ten String)
let responseDictionary = ["message":response] // tohle zase vyrobí slovník "message":String
NSNotificationCenter.defaultCenter().postNotificationName(notificationWatch, object: nil)
reply(responseDictionary)
}
}
如您所见,我使用通知让 iOS 应用程序知道按钮已被点击。在 ViewController.swift 中,我有 Notification Observer 和函数,每次观察者捕捉到用户点击手表按钮的通知时都会执行("notificationWatch" 是带有通知键的全局变量)。希望这对任何人都有帮助。