IOS - WatchKit, how to send message / data from iPhone app to WatchKit app?

I am creating a WatchKit app and wondered how to send message / data from iPhone to Watch?

I know how to do it differently (watch → phone) using " openParentApplication: reply: " and " application: handleWatchKitExtensionRequest: reply: " but cannot find documentation on how to talk from the phone to watch.

A simple setup would be for an iPhone application with a button that, when clicked, should update the shortcut in the Watch application.

Can someone point me in the right direction?

+4
source share
5 answers

First, you need to enable application groups for your purpose:

enter image description here

Then you can start writing and reading objects through NSUserDefaults:

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


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

See “Sharing data with your iOS software” in the Apple Watch Programming Guide: Design for Apple Watch

+3
source

This works for me. Try using a 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 the main application

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

, iOS .

Apple Watch:

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

:

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

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

"AppShare" - , .

+1

,

, , (Extension) iOS.

@zisoft, .

URL- ,

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

/ , ,

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");
}
0

WatchOS 2.0 , Watch Connectivity Framework, .

This structure provides a bi-directional communication channel for sending files and data dictionaries between two processes

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

A WiKi example is also available .

Good luck.

0
source

All Articles