且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过Apple Watch更新app内容的简便方法

更新时间:2023-12-03 18:51:28

你不幸运,因为当你写下你的问题时,没有API在iOS SDK中。三天前,2014年12月10日Apple发布了iOS 8.2 beta 2 SDK,其中有两个对此任务非常重要,方法。

You weren't lucky because when you wrote your question there wasn't API for this in iOS SDK. Three days ago, 10th December 2014 Apple released iOS 8.2 beta 2 SDK with two, important for this task, methods.

在WatchKit Framework中, WKInterfaceController class

In WatchKit Framework, WKInterfaceController class

// Obj-C
+ (BOOL)openParentApplication:(NSDictionary *)userInfo
                        reply:(void (^)(NSDictionary *replyInfo,
                                        NSError *error))reply

通过调用此方法,iOS将在后台运行您的应用程序,应用程序的AppDelegate将收到此消息

By calling this method iOS will run your app in the background and AppDelegate of app will receive this message

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply. 

这是UIKit Framework中iOS SDK Beta 2(就此问题而言)中添加的第二种方法, UIApplicationDelegate class。

This is second method added in iOS SDK Beta 2 (in terms of this question) in UIKit Framework, UIApplicationDelegate class.

您可以使用NSDictionary和回复块来与Watch app和iOS app进行通信。

You can use NSDictionary and reply block to communicate Watch app and iOS app.

示例

WKInterfaceController 中子类

- (IBAction)callPhoneAppButtonTapped
{
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"text to display", @"key", nil];

    [InterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) {
        NSLog(@"Reply received by Watch app: %@", replyInfo);
    }];
}

然后在你的iOS AppDelegate课程中

and then in your iOS AppDelegate class

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
    NSLog(@"Request received by iOS app");
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"your value to return to Apple Watch", @"key", nil];

    reply(dictionary);
}

点击Apple Watch模拟器上的按钮时,iOS模拟器中的iOS应用程序将会是推出,你应该能够在适当的地方看到NSLog。

When you tap button on Apple Watch simulator your iOS app in iOS Simulator will be launched, and you should be able to see NSLog's in proper places.

注意

此解决方案适用于在Watch和iOS应用程序之间传输对象。
但是如果您打算传输更多数据,访问图像,文件等,您应该使用共享应用程序组。您可以在Xcode项目文件中的Capabilities中设置共享应用程序组。
使用 containerURLForSecurityApplicationGroupIdentifier NSFileManager class)获取共享组中文件的URL。

This solution works for transporting objects between Watch and iOS apps. But if you plan to transport more data, access images, file etc, you should use Shared app group. You set shared app group in Capabilities in your Xcode Project file. Use containerURLForSecurityApplicationGroupIdentifier (NSFileManager class) to get URLs to files in shared group.

如果你想从 NSUserDefaults 分享首选项 initWithSuiteName 就是你正在寻找的for。

If you want to share Preferences initWithSuiteName from NSUserDefaults is what you are looking for.