handleWatchKitExtensionRequest 未响应 Watchkit 扩展中的 openParentApplication (Swift)

handleWatchKitExtensionRequest not responding to openParentApplication in Watchkit Extension (Swift)

我正在尝试将信息从我的 WatchKit 应用程序发送到我的主要父应用程序,据我所知,我应该只能在我的 watchkit 扩展中使用 openParentApplication,它将由 [=14] 接收=] 在 AppDelegate.swift 中,但我似乎无法触发 handleWatchKitExtensionRequest

我遇到了一些问题,所以此时我只是尝试建立任何连接,然后再担心实际传递的信息是什么。所以目前在我的 Watchkit ViewController 我有以下内容:

 let testDict = [
    "value1" : "Test 1",
    "value2" : "Test 2"
]

@IBAction func saveButtonFunction() {
    openParentAppForBalance(testDict)
}

private func openParentAppForInfo(Dict: [String: String]) {

    WKInterfaceController.openParentApplication(testDict,
        reply: {(reply, error) -> Void in
            println("openParentApplication called in button function")
    })
}

在输出中显示正在调用该函数,但 handleWatchKitExtensionRequest 只是不会响应。目前它在 AppDelegate.swift 中设置为以下永远不会被调用的内容:

func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) {

    println("we made it!")

    var retValues = Dictionary<String,String>()

    retValues["retval1"] = "return Test 1"
    retValues["retval1"] = "return Test 2"

    reply(retValues)

}

我敢肯定,在我对这一切工作原理的理解中,我可能只是遗漏了一些真正基础的东西,但如果能提供有关如何触发 handleWatchKitExtensionRequest 的任何帮助,我们将不胜感激!

啊,我认为这里发生的事情是你的代码既正确又能正常工作,你在这里解释的是两个完全可以理解的假设重叠的结果,这实际上是不正确,一直在误导你。好消息是,您的代码已经可以正常工作了。

你说,

...which shows in the output that the function is being called...

如果您的意思是您在控制台中看到消息,openParentApplication called in button function,那么这是正在发生的事情:

您的这部分代码是 Swift 闭包:

{(reply, error) -> Void in
        println("openParentApplication called in button function")
}

当您的 WatchKit Extension 调用 WKInterfaceController.openParentApplication 时,它会将字典(您的 testDict)传递给您的父 iPhone 应用程序,iPhone 应用程序可以使用它来 return 数据给你——前提是数据已经序列化。它还 return 返回给您传递给它的闭包。这使您的 WatchKit Extension to 运行 代码能够在稍后收到回复时自行定义。您可以在此闭包中使用 testDict 中的 returned 数据以及调用 openParentApplication 时可在本地访问的其他变量。您的 WatchKit Extension 在收到返回时自动执行闭包中的代码。

所以当你看到openParentApplication called in button function的时候,这说明已经收到了来自iPhone申请的回复,并且已经执行了闭包。因此,您的 WatchKit 测试代码应该真正将 println 语句更改为:

WKInterfaceController.openParentApplication(testDict,
    reply: {(reply, error) -> Void in
        println("Reply to openParentApplication received from iPhone app")
    })

现在,您没有意识到代码正确执行的原因是可以理解的,因为您希望在控制台中看到拒绝此代码已在您的 iPhone 应用程序中执行:

println("we made it!")

但是,Xcode 不支持同时附加到两个进程。因此,当您附加到 WatchKit 应用程序时,您将看不到 iPhone 应用程序的任何日志消息。如果不是附加进程,您的 iPhone 应用程序也不会响应断点。无论是 运行ning 在后台(被 openParentApplication 唤醒)还是在前台 运行ning(如果您在 WatchKit 应用程序启动后在模拟器中手动启动它),这两个都是正确的运行。您可以看到 iPhone 应用程序 activity 的效果,但无法在附加到 WatchKit 应用程序时直接内省它。

首先,您的代码运行正常。你可以移过去你的测试代码!关于在 iPhone 端响应您的 WatchKit 应用程序时对其工作进行内省,有一个部分解决方案。从模拟器启动 WatchKit 应用程序,一旦它 运行ning,在 Xcode 中激活菜单选项 Debug > Attach to process... 和 select 您的 iPhone 应用进程位于顶部的 可能目标 下。现在您将看到您的 iPhone 应用程序控制台消息,并且您的 iPhone 应用程序将响应断点——但是您当然不会再从 WatchKit 应用程序端看到这些。您继续能够在模拟器中与这两个应用程序进行交互,并且可以在执行期间在您所连接的应用程序之间来回切换。

附带说明一下,您实际上可以在 Xcode 中同时附加到两个进程,这对于调试 WatchKit 非常有用。从 Xcode 构建并 运行 您的 WatchKit 扩展目标,它将在模拟器中启动 运行ning。现在,在模拟器中,启动您的 iOS 应用程序。使用那个 运行ning,转到调试->附加到进程->[您的应用程序进程的名称]。现在,您将在 Xcode 中看到两个进程 运行ning,并且您将能够记录、调试等...跨越这两个进程。