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

查看:294
本文介绍了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 App启动之前.

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

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天全站免登陆