Ios 如何从iPhone上的手表触发后台进程(触发器:手表)?

Ios 如何从iPhone上的手表触发后台进程(触发器:手表)?,ios,swift,background-process,uilocalnotification,watchkit,Ios,Swift,Background Process,Uilocalnotification,Watchkit,我想在我的手表应用程序中添加一项功能,向iPhone应用程序发送本地通知(当iPhone应用程序处于后台或iPhone被锁定时) 我知道如何创建本地通知本身 我要问的是,如何通过(例如)点击Apple Watch上的按钮在iPhone上触发后台进程(也包含本地通知)。WKInterfaceController.openParentApplication是与iPhone通信的官方方式 您可以在userInfo字典中传递参数,并通过reply块检索结果 在iPhone上,请求由appDelegate

我想在我的手表应用程序中添加一项功能,向iPhone应用程序发送本地通知(当iPhone应用程序处于后台或iPhone被锁定时)

我知道如何创建本地通知本身


我要问的是,如何通过(例如)点击Apple Watch上的按钮在iPhone上触发后台进程(也包含本地通知)。

WKInterfaceController.openParentApplication
是与iPhone通信的官方方式

您可以在
userInfo
字典中传递参数,并通过
reply
块检索结果


在iPhone上,请求由appDelegate的
handleWatchKitExtensionRequest
方法处理

WKInterfaceController.openParentApplication
是与iPhone通信的官方方式

您可以在
userInfo
字典中传递参数,并通过
reply
块检索结果


在iPhone上,请求由appDelegate的
handleWatchKitExtensionRequest
方法处理

我的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中,我有通知观察者和函数,每当观察者捕捉到用户点击手表按钮的通知时,就会执行该函数(“notificationWatch”是带有通知键的全局变量)。希望这对任何人都有帮助。

我的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中,我有通知观察者和函数,每当观察者捕捉到用户点击手表按钮的通知时,就会执行该函数(“notificationWatch”是带有通知键的全局变量)。希望这对任何人都有帮助。

可能的重复