iOS – WatchKit how to send message/data from iPhone app to WatchKit app?

5 Solutions Collect From Internet About “iOS – WatchKit how to send message/data from iPhone app to WatchKit app?”

First, you have to enable app groups for your target:

enter image description here

Then you can start to write and read objects via NSUserDefaults:

// write 
let sharedDefaults = NSUserDefaults(suiteName: appGroupName)
sharedDefaults?.setInteger(1, forKey: "myIntKey")


// read
let sharedDefaults = NSUserDefaults(suiteName: appGroupName)
let myIntValue = sharedDefaults?.integerForKey("myIntKey")

See the chapter Sharing Data with Your Containing iOS App in Apple Watch Programming Guide: Developing for Apple Watch

It’s work for me. Try use in watch

- (void)registerToNotification
{
    [ self unregisterToNotification ];
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), didReceivedDarwinNotification, CFSTR("NOTIFICATION_TO_WATCH"), NULL, CFNotificationSuspensionBehaviorDrop);
}

- (void)unregisterToNotification
{
    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), CFSTR( "NOTIFICATION_TO_WATCH" ), NULL );
}

void didReceivedDarwinNotification()
{
    // your code
}

in main app

- (void)sendNotificationToWatch:(NSDictionary*)info
{
    CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("NOTIFICATION_TO_WATCH"), (__bridge const void *)(self), nil, TRUE);
}

You should try App Groups which are what you use to share data between iOS apps and App Extensions.

In your Apple Watch app interface controller class:

    let sharedDefaults = NSUserDefaults(suiteName: "group.com.<domain>.<appname>.AppShare")
    sharedDefaults?.setObject("Came from Apple Watch App", forKey: "AppleWatchData")
    sharedDefaults?.synchronize()

In your parent app:

    let sharedDefaults = NSUserDefaults(suiteName: "group.com.<domain>.<appname>.AppShare")

    if let appWatchData = sharedDefaults?.objectForKey("AppleWatchData") as? NSString {
        println(appWatchData)
    }

“AppShare” is the name you assign when you create an App Group in Capabilities for your parent app target.

Alternatively,

You can use this solution to share files even between 2 different apps and of course between watch app (Extension) and parent iOS app.

First step is described by @zisoft, enable app group.

Then get the URL of group container at runtime,

- (NSString *)containerPath
{
    return [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"YOUR_APP_GROUP"] relativePath];
}

Now you can write any file/folder at the given path, Below is my example snippet,

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.array];

NSString *path = [[[self containerPath] stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@"history"];

if ([data writeToFile:path atomically:YES])
{
    NSLog(@"Success");
}
else
{
    NSLog(@"Failed");
}

watchOS 2.0 has a new framework which is called Watch Connectivity Framework that let you send messages between the two devices.

That framework provides a bidirectional communications channel for sending files and data dictionaries between the two processes

Please see the example here including the example of sending the actual dictionary using debug mode.

A WiKi example is also available.

Good luck.