可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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
回答2:
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);
}
回答3:
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.
回答4:
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.
回答5:
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");
}