handleWatchKitExtensionRequest没有响应Watchkit扩展中的openParentApplication(Swift) [英] handleWatchKitExtensionRequest not responding to openParentApplication in Watchkit Extension (Swift)

查看:89
本文介绍了handleWatchKitExtensionRequest没有响应Watchkit扩展中的openParentApplication(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将WatchKit App中的信息发送到我的主要父应用程序,据我所知,我应该能够在我的watchkit扩展中使用 openParentApplication 将在AppDelegate.swift中通过 handleWatchKitExtensionRequest 收到,但我似乎无法触发 handleWatchKitExtensionRequest

I am trying to send information from my WatchKit App over to my main parent application and from what I understand I should just be able to use openParentApplication in my watchkit Extension which will be received by handleWatchKitExtensionRequest in AppDelegate.swift, but I cant seem to get handleWatchKitExtensionRequest to be triggered.

我一直有一些问题,所以在这一点上我只是想要建立任何连接,然后再担心实际传递了什么信息。所以目前在我的Watchkit ViewController中我有以下内容:

I've been having some issues, so at this point I'm just trying to establish any connection at all before worrying about what information is actually passed. so currently in my Watchkit ViewController I have the following:

 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中设置为以下内容,它永远不会被调用:

which shows in the output that the function is being called, but handleWatchKitExtensionRequest just wont respond. Currently it's set to the following in AppDelegate.swift which never gets called:

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 的任何帮助都将是巨大的赞赏!

I'm sure I'm probably just missing something really fundamental here in my understanding of how this all works, but any help at all about how to get handleWatchKitExtensionRequest to be triggered would be hugely appreciated!

推荐答案

啊,我认为这里发生的事情是你的代码是正确的,并且完全正常工作,你在这里解释的是一个结果这是两个完全可以理解的假设的重叠,这些假设实际上是不正确的并且一直让你误入歧途。好消息是,你的代码已经运行了。

Ah, I think what is happening here is that your code is both correct, and working exactly as it should, and what you're interpreting here is a result of an overlap of two entirely understandable assumptions, that are actually not correct and have been leading you astray. So the good news is, your code is already working.

你说,


...在输出中显示正在调用该函数...

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

如果你那意味着在控制台你看到消息,在按钮功能中调用openParentApplication,然后在这里发生了什么:

If by that you mean that in the console you are seeing the message, openParentApplication called in button function, then here's what is going on:

这部分你的代码是Swift Closure:

This part of your code is a Swift Closure:

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

当您的WatchKit扩展调用 WKInterfaceController.openParentApplication 时,它会将一个字典(您的 testDict )传递给您的父iPhone应用程序, iPhone应用程序可以用来向您返回数据 - 只要数据已经序列化。它还会向您返回传递它的闭包。这使得WatchKit扩展能够在收到回复的稍后时刻运行它自己定义的代码。您可以在此闭包中使用 testDict 中的返回数据以及当时可在本地访问的其他变量 openParentApplication 被叫了。收到回来后,你的WatchKit扩展会自动执行闭包中的代码。

When your WatchKit Extension calls WKInterfaceController.openParentApplication it passes to your parent iPhone app a dictionary (your testDict), which the iPhone application can use to return data to you—providing the data has been serialized. It also returns back to you the closure that you passed it. This enables your WatchKit Extension to run code that it has itself defined, at the later point when the reply has been received. You can include in this closure use of both the returned data in the testDict and also other variables that were locally accessible at the time openParentApplication was called. Your WatchKit Extension automatically executes the code in the closure when it is received back.

所以当你看到在按钮函数中调用openParentApplication时,这表示已收到来自iPhone应用程序的回复,并且已执行关闭。因此,您的WatchKit测试代码应该真正将println语句更改为:

So when you see openParentApplication called in button function, this indicates that the reply from the iPhone application has been received, and the closure has been executed. Therefore, your WatchKit test code should really change the println statement to be:

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

现在,您可以理解为什么代码没有正确执行的原因是因为您希望在控制台中看到此代码已在您的iPhone中执行被拒绝app:

Now, the reason why you quite understandably didn't realise the code was executing correctly was because you were expecting to see rejected in the console that this code had been executed in your iPhone app:

println("we made it!")

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

However, Xcode does not support attaching to two processes simultaneously. Therefore, when you are attached to your WatchKit app, you will not see any log messages of your iPhone app. Your iPhone app also will not respond to breakpoints when it is not the attached process. Both of these are true whether it is running in the background (woken by openParentApplication) or running in the foreground (if you launch it manually in the simulator after the WatchKit app has been run. You can see the effects of the iPhone app activity, but cannot directly introspect it while you are attached to the WatchKit app.

首先,您的代码工作正常你可以超越你的测试代码!而且当它响应你的WatchKit应用程序时反省iPhone方面的工作,有一个部分解决方案。从模拟器启动WatchKit应用程序,一旦它运行, Xcode激活菜单选项调试>附加以处理... 并在顶部的可能目标下选择您的iPhone应用程序进程。现在您将看到您的iPhone应用程序控制台消息和你的iPhone应用程序将响应断点 - 当然你将不再从WatchKit应用程序端看到这些。你继续能够与模拟器中的两个应用程序进行交互,并且可以在执行期间来回切换您所连接的应用程序。

So firstly, your code is working correctly. You can move past your test code! And in relation to introspecting the workings in the iPhone side when it is responding to your WatchKit app, there is a partial solution. Launch the WatchKit app from the simulator, and once it is running, in Xcode activate the menu option Debug > Attach to process... and select your iPhone app process under Likely targets at the top. Now you will see your iPhone app console messages and your iPhone app will respond to breakpoints—but of course you will no longer see these from the WatchKit app side. You continue to be able to interact with both apps in the simulators, and can swap back and forth between which one you are attached to during execution.

这篇关于handleWatchKitExtensionRequest没有响应Watchkit扩展中的openParentApplication(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆