Apple Watch, WatchKit extension, and main application

There is a main application with logic, and we are expanding the application to the Apple Watch.

After adding the target, xCode creates 2 more applications: an extension with an application of code and a set of hours.

Question: How can code from the extension reuse the logic of a finished and already made main iOS application? How an extension application can interact with basic application and send commands.

+7
main ios watchkit
source share
2 answers

To contact the containing iPhone application, you can use

(BOOL)openParentApplication:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *replyInfo, NSError *error))reply 

In WKInterfaceController

From Apple Docs

Use this method to communicate with your iOS application. The calling method causes iOS to start the application in the background (if necessary) and call the application:handleWatchKitExtensionRequest:reply : method of its application delegate. This method has the following signature:

 - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply 

The application delegate receives the dictionary that you pass to userInfo and uses it to process any request you requested. If it provides a response, WatchKit executes the block that you provided in the response parameter of this method.

+9
source share

In the current state of the Apple Watch extension:

  • You can exchange information between the main iOS application and the WatchKit extension. Use application groups and NSUserDefaults to access shared information objects.

  • You cannot execute code from an iOS application that launches from actions on the Apple Watch.

At least not yet.

EDIT: Starting with Xcode 6.2 Beta 2

You can now chat with the parent watch iOS app from Apple Watch.

In WatchKit Extension, call the parent application through openParentAppentApplicion . You can pass the value dictionary to the parent application, and the parent application can return the value dictionary.

Watchkit Extension:

 // Call the parent application from Apple Watch // values to pass let parentValues = [ "value1" : "Test 1", "value2" : "Test 2" ] WKInterfaceController.openParentApplication(parentValues, reply: { (replyValues, error) -> Void in println(replyValues["retVal1"]) println(replyVaiues["retVal2"]) }) 

IOS app:

 // in AppDelegate.swift func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)!) { // retrieved parameters from Apple Watch println(userInfo["value1"]) println(userInfo["value2"]) // pass back values to Apple Watch var retValues = Dictionary<String,String>() retValues["retVal1"] = "return Test 1" retValues["retVal2"] = "return Test 2" reply(retValues) } 
+3
source share

All Articles