WatchOS 2.0 在 iOS App 和 WatchOS App 之间共享数据 [英] WatchOS 2.0 Sharing Data between iOS App and WatchOS App

查看:29
本文介绍了WatchOS 2.0 在 iOS App 和 WatchOS App 之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前我使用 NSUserDefaults 在手表和应用程序之间共享基本变量.

Previous I used NSUserDefaults to share basic variables between the watch and app.

我的目标是在手表上显示用户的电子邮件地址,该地址存储在 iPhone 应用程序 NSUserDefaults 中.我了解在 Watch v1 之后,您不能再使用应用组来共享数据.

My goal is to display the user's email address on the watch which is stored on the iPhone app NSUserDefaults. I understand that after Watch v1, you cannot use App Groups anymore to share data.

我知道您需要使用 Watch Connectivity API,但我只能弄清楚如何在操作发生时使用它.不是在 Watch 应用启动之前.

I understand you need to use the Watch Connectivity API but I can only figure out how to use it when an action happens. Not before the Watch App is started.

如果您有任何想法,请告诉我.

Let me know if you have any ideas.

推荐答案

是的,您可以将电子邮件存储在 iPhone 上的 NSUserDefaults 中,并在手表上显示.为此,您需要使用 Watch Connectivity API 向 iPhone 发送消息,当您收到此消息时,使用从 NSUserDefaults 获取的电子邮件地址进行响应.这可能会在 Watch 上的应用程序启动后发生,但不会在此之前发生.如果您不想每次都向 iPhone 询问,您可以将其缓存在 Watch 上以备将来使用.如果它在 iPhone 上发生变化,您也可以向 Watch 发送消息以更新它.

Yes, you can store the email in NSUserDefaults on iPhone, and display it on the Watch. To do so, you need to send a message to the iPhone using the Watch Connectivity API, and when you receive this message respond with the email address you grab from NSUserDefaults. This can occur once the app on the Watch is launched, but not before then. You could cache it on the Watch for future use if you don't wish to have to ask the iPhone for it every time. If it changes on the iPhone you can message the Watch to update it there as well.

以下是一些示例代码,用于展示如何使用 Swift 从 Apple Watch 发出请求并使用 Objective-C 在 iPhone 上做出响应.

Here's some example code to show how to make the request from the Apple Watch in Swift and respond to it on the iPhone in Objective-C.

启动后 Watch 应用代码中的某处:

Somewhere in your Watch app's code after launch:

if WCSession.defaultSession().reachable {
    WCSession.defaultSession().sendMessage(["email-request": "email"], replyHandler: { (reply: [String : AnyObject]) -> Void in
        //success
        dispatch_async(dispatch_get_main_queue()) {
            if let email = reply["email-request"] as? String {
                //store the email, display it, etc
            }
        }
    }, errorHandler: { (error: NSError) -> Void in
        dispatch_async(dispatch_get_main_queue()) {
            //couldn't get email, maybe show error message
        }
    })
} else {
    //iPhone not available, maybe show error message
}

在您的 iPhone 应用程序的 AppDelegate 中:

In your iPhone app's AppDelegate:

- (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (message[@"email-request"]) {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            replyHandler(@{@"email-request": @([defaults stringForKey:@"email"])});
        }
    });
}

请注意,应用程序委托和扩展委托都必须采用WCSessionDelegate 协议,并且需要正确激活它们.请参阅观看连接框架参考了解更多信息.

Note that both the app delegate and extension delegate have to adopt the WCSessionDelegate protocol and they need to be properly activated. See the Watch Connectivity Framework Reference for additional information.

这篇关于WatchOS 2.0 在 iOS App 和 WatchOS App 之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆