7

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.

vitalii
  • 191
  • 2
  • 8
  • Your question is too broad. Please be more specific with questions. As per the documentation, you will have a main app that runs on the phone, an app extension that runs on the phone, and a watchkit bundle which are resources shuttled over to the watch for display on the watch. None of your code actually runs on the watch. You are just providing a resource bundle and data to the watch. – Jeremy Huddleston Sequoia Nov 28 '14 at 19:48
  • 1
    You can use App Groups or the https://github.com/mutualmobile/MMWormhole. – kelin Mar 16 '15 at 10:56

2 Answers2

11

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.

Stephen Johnson
  • 5,293
  • 1
  • 23
  • 37
  • 1
    For a code example on how to use the background mode, which ensures that the main app has time to send its reply, refer to "Calling parent application from Watch app" (http://stackoverflow.com/questions/30000274/calling-parent-application-from-watch-app/30000323#30000323) – John May 02 '15 at 09:46
4

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)
}
zisoft
  • 22,770
  • 10
  • 62
  • 73
  • Thank you zisoft, is there a way to implement event-listener and trigger function in memmory between extention and host app? – vitalii Nov 28 '14 at 20:37
  • I assume you are thinking about something like this: Press a button on the Apple Watch and start an action on the iPhone. This would require to keep the iOS app running and polling for some information of whatever kind. I didn't find a way to do this so far. Maybe sometimes, this is very new stuff... – zisoft Nov 28 '14 at 20:42
  • I have created Pull service which watches values we have stored in the NSUserDefaults and if they have changed, notifies listeners. Problem: It seems that values are being changed only when application starts, restarts. Any ideas? – vitalii Dec 01 '14 at 17:36
  • It's also possible to pass information between an app and its extensions by updating files in a shared app group container. You can then write to the file in one process, and use `NSFilePresenter` to get notified in the other process. – Joe Trellick Dec 07 '14 at 14:24