HandleWatchKitExtensionRequest does not respond to openParentApplication in Watchkit extension (Swift)

I am trying to send information from my WatchKit application to my main parent application and, as I understand it, should just use openParentApplication in my watchkit extension, which will get handleWatchKitExtensionRequest in AppDelegate.swift, but I cannot run handleWatchKitExtensionRequest .

I had some problems, so at this point I just try to establish some kind of connection at all, before worrying about what information is actually transmitted. so 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") }) } 

which shows in the output that the function is being called, but handleWatchKitExtensionRequest just isn't responding. It is currently installed in AppDelegate.swift, which is never 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) } 

I'm sure I probably just missed something really fundamental in my understanding of how it all works, but any help whatsoever on how to get handleWatchKitExtensionRequest to run would be greatly appreciated!

+6
source share
2 answers

And, I think that what happens here is that your code is correct and works exactly as it should, and what you interpret here is the result of overlapping two perfectly understandable assumptions that are actually incorrect and led you astray. So the good news: your code is already running.

You speak,

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

If by this you mean that in the console you see a message, openParentApplication called in button function , then this is what happens:

This part of your code is a quick close:

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

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

So, when you see the openParentApplication called in button function , it means that a response from the iPhone application has been received and the closure has been completed. Therefore, your WatchKit test code should really change the println statement:

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

Now, the reason you didnโ€™t understand in a perfectly understandable way that the code was executing correctly was because you expected to see the rejected console that this code was running in your iPhone application:

 println("we made it!") 

However, Xcode does not support simultaneous joining of two processes. Therefore, when you are tied to your WatchKit application, you will not see any log messages of your iPhone application. Your iPhone application will also not respond to breakpoints if it is not an attached process. Both of them are true, whether it works in the background ( openParentApplication wakes up) or works in the foreground (if you run it manually in the simulator after starting the WatchKit application. You can see the effects of the iPhone application, but cannot directly introspect it when you are attached to WatchKit app.

So, firstly, your code is working correctly. You can get past your test code! And regarding the introspection of working on the iPhone side, when it answers your WatchKit app, there is a partial solution. Launch the WatchKit application from the simulator, and as soon as it starts, in Xcode, activate the menu option Debug> Attach to the process ... and select your iPhone application in Probable targets at the top. You will now see your iPhone console application messages, and your iPhone application will respond to breakpoints, but of course you will no longer see them from the WatchKit application. You can still interact with both applications in the simulators and can switch back and forth between which you are connected at runtime.

+25
source

Just note that you can actually connect to two processes simultaneously in Xcode, and this is very useful for debugging WatchKit. Create and run the WatchKit Extension target from Xcode and it will start working in the simulator. Now, in the simulator, launch the iOS application. After that go to Debug-> Attach to Process โ†’ [Name of your app process]. Now you will see both processes running in Xcode, and you will be able to log, debug, etc. On both.

+15
source

Source: https://habr.com/ru/post/981455/


All Articles