简体   繁体   English

iOS - WatchKit如何将消息/数据从iPhone应用程序发送到WatchKit应用程序?

[英]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? 我正在创建一个WatchKit应用程序,并想知道如何从iPhone发送消息/数据到手表?

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. 我知道如何使用' openParentApplication:reply: '和' application:handleWatchKitExtensionRequest:reply: '来反过来 (手表 - >手机),但找不到任何关于如何通过手机进行通信的文档。

Simple setup would be the iPhone app has a button that when pressed should update a label on the Watch app. 简单的设置将是iPhone应用程序有一个按钮,按下时应该更新Watch应用程序上的标签。

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 : 然后您可以通过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 请参阅“ Apple Watch编程指南:为Apple Watch开发”中的“ 与包含iOS应用程序共享数据 ”一章

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. 您应该尝试使用App Groups,它们用于在iOS应用和App Extensions之间共享数据。

In your Apple Watch app interface controller class: 在Apple Watch应用程序界面控制器类中:

    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. “AppShare”是您为父应用目标在功能中创建应用组时指定的名称。

watchOS 2.0 has a new framework which is called Watch Connectivity Framework that let you send messages between the two devices. watchOS 2.0有一个新的框架,称为Watch Connectivity Framework ,可让您在两个设备之间发送消息。

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. 您可以使用此解决方案甚至在2个不同的应用程序之间共享文件,当然还可以在监视应用程序(Extension)和父iOS应用程序之间

First step is described by @zisoft, enable app group. 第一步由@zisoft描述,启用应用程序组。

Then get the URL of group container at runtime, 然后在运行时获取组容器的URL,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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