简体   繁体   中英

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

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

I know how to do it the other way around (watch -> phone) using ' openParentApplication:reply: ' and ' application:handleWatchKitExtensionRequest:reply: ' but can't find any documentation on how to communicate from phone to watch.

Simple setup would be the iPhone app has a button that when pressed should update a label on the Watch app.

Can anyone point me in the right direction?

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

在此输入图像描述

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM