You weren't lucky because when you wrote your question there wasn't API for this in iOS SDK. Three days ago, 10th December 2014 Apple released iOS 8.2 beta 2 SDK with two, important for this task, methods.
In WatchKit Framework, WKInterfaceController
class
// Obj-C
+ (BOOL)openParentApplication:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *replyInfo,
NSError *error))reply
By calling this method iOS will run your app in the background and AppDelegate of app will receive this message
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply.
This is second method added in iOS SDK Beta 2 (in terms of this question) in UIKit Framework, UIApplicationDelegate
class.
You can use NSDictionary and reply block to communicate Watch app and iOS app.
Example
In your WKInterfaceController
subclass
- (IBAction)callPhoneAppButtonTapped
{
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"text to display", @"key", nil];
[InterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) {
NSLog(@"Reply received by Watch app: %@", replyInfo);
}];
}
and then in your iOS AppDelegate class
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
NSLog(@"Request received by iOS app");
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"your value to return to Apple Watch", @"key", nil];
reply(dictionary);
}
When you tap button on Apple Watch simulator your iOS app in iOS Simulator will be launched, and you should be able to see NSLog's in proper places.
Note
This solution works for transporting objects between Watch and iOS apps.
But if you plan to transport more data, access images, file etc, you should use Shared app group
. You set shared app group in Capabilities in your Xcode Project file.
Use containerURLForSecurityApplicationGroupIdentifier
(NSFileManager
class) to get URLs to files in shared group.
If you want to share Preferences initWithSuiteName
from NSUserDefaults
is what you are looking for.