Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple Watch, WatchKit Extension and main application

Tags:

main

ios

watchkit

There is main application with logic and we extend app to Apple Watch.

After adding target xCode creates 2 more applications: extension with code and watch kit application.

Question: How code from extension can reuse logic of ready and already made main iOS app? How extension app can communicate with main App and send commands.

like image 297
vitalii Avatar asked Nov 28 '14 14:11

vitalii


People also ask

What is WatchKit extension?

A WatchKit Extension process that executes the watch app's application logic. This is bundled with your Watch app and runs on the Apple Watch. A Watch app that displays user interface elements and handles navigation.

Where does the watch app extension code run in watchOS 2?

In watchOS 2, not only does the WatchKit extension run on the user's Apple Watch, it is delivered inside the user's Watch app.

How do I get the Xcode app for Apple Watch?

To build a watch-only app, start a new project in Xcode, select the Watch App template, and click Next. Provide a name, and choose the interface, life cycle, and language for your watch app. Select the options to include a notification scene template or unit and user interface tests, and click Next.


2 Answers

To communicate to the containing iPhone app you can use

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

In your WKInterfaceController

From the Apple Docs

Use this method to communicate with your containing iOS app. Calling the method causes iOS to launch the app in the background (as needed) and call the application:handleWatchKitExtensionRequest:reply: method of its app delegate. That method has the following signature:

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

The app delegate receives the dictionary you pass into the userInfo parameter and uses it to process whatever request you made. If it provides a reply, WatchKit executes the block you provided in the reply parameter of this method.

like image 73
Stephen Johnson Avatar answered Sep 27 '22 16:09

Stephen Johnson


At current state of Apple Watch Extension:

  • You can share information between iOS main appliation and WatchKit Extension. Use App Groups and NSUserDefaults to access the shared information objects.

  • You can not execute code from your iOS app which is trigged from actions on the Apple Watch.

At least not yet.

EDIT: As of Xcode 6.2 Beta 2

It is now possible to communicate with the parent iOS app from Apple Watch.

In WatchKit Extension call the parent application via openParentAppentApplicion. One can pass a value dictionary to the parent application and the parent application can return a 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)
}
like image 38
zisoft Avatar answered Sep 27 '22 15:09

zisoft