使用Watch连接框架将数据从iPhone传输到iWatch [英] Data transfering from iPhone to iWatch with Watch connectivity framework

查看:258
本文介绍了使用Watch连接框架将数据从iPhone传输到iWatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个关于手表连接性的问题.

I want to ask a question about watch connectivity.

1)打开iWatch应用程序时是否可以从iPhone读取数据.我不想等待打开iPhone应用程序以将数据传输到iWatch.

1) Is it possible to read data from iPhone when the iWatch app opened. I not want to wait to open iPhone app for transfering data to iWatch.

2)是否可以在iWatch上创建登录屏幕(从文本字段获取用户输入)

2) Is it possible to create login screen(to get user input from text fields) on iWatch

3)iWatch是否具有设备令牌和供应商ID?如何从iWatch获取这些信息?

3) iWatch has device token and vendor id? How to get these infos from iWatch?

4)是否可以从iWatch应用程序读取iPhone应用程序的数据库(如iPhone应用程序上的sql lite db)

4) Is it possible to read iPhone app's database(like sql lite db on iPhone app) from iWatch application

5)如何将字典从iPhone应用程序转移到iWatch应用程序.分享任何示例plz.

5) How to transfer dictionary from iPhone app to iWatch app. Share any example plz.

推荐答案

1)打开iWatch应用程序时是否可以从iPhone读取数据.我不想等待打开iPhone应用程序以将数据传输到iWatch.

是,使用任何后台方法(transferUserInfo:transferCurrentComplicationUserInfo:transferFile:updateApplicationContext:infoToSend),您都可以唤醒iPhone应用并完成工作.反之亦然,必须打开Watch应用.

YES, Using any of background methods (transferUserInfo:, transferCurrentComplicationUserInfo:, transferFile:,updateApplicationContext:infoToSend ) you can awake iPhone app and get things done. vice versa is not possible Watch app has to be Opened.

2)是否可以在iWatch上创建登录屏幕(从文本字段获取用户输入)

否,WatchOS2中的文本字段不可用.

NO, Text fields are not available in WatchOS2.

3)iWatch是否具有设备令牌和供应商ID?如何从iWatch获取这些信息?

在watchOS 1上,由于WatchKit扩展本身在iPhone上运行,因此供应商ID和广告ID实际上在iPhone上.

With watchOS 1, the vendor ID and the advertising ID were actually on the iPhone as the WatchKit extension itself ran on the iPhone.

使用watchOS 2,您需要将iPhone的供应商ID和广告ID同步到Watch并在其中使用. 并且您将需要保持供应商ID和广告ID为最新.

With watchOS 2, you will need to sync the vendor ID and advertising ID from the iPhone to the Watch and use it there. And you will need to maintain the vendor ID and advertising ID up-to-date.

4)是否可以从iWatch应用程序读取iPhone应用程序的数据库(如iPhone应用程序上的sql lite db)

虽然可以在WatchKit中使用它,但是由于引入了WatchConnectivity Framework应用程序,基于应用组的通用容器受到了限制.我确定可以使用UserDefualts,但尚未对文件进行测试.

It was possible in WatchKit but with the introduction of WatchConnectivity Framework App group based common container has been restricted.I am sure for UserDefualts but not have yet tested for Files.

5)如何将字典从iPhone应用程序转移到iWatch应用程序.共享任何示例plz.

有两种方法可以执行这些操作:

There are Two ways to perform these things:

使用TransferUserInfo

使用此方法,Watch每次都会收到字典,这意味着,如果Watch处于非活动状态,并且iPhone在该时间段内发送了3个字典,则只要Watch激活,它将通过多次调用委托方法来接收所有3个字典-在手表端.

With this method, Watch will receive dictionary everytime, that means if Watch is inactive and iphone sends 3 Dictionary during that time period, Whenever watch will activate, it will receive all the 3 dictionary by multiple calls of delegate methods - - (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *, id> *)userInfo on watch side.

-(void)sendDictionaryToWatch:(NSDictionary*)infoToSend{
        if([WCSession isSupported]){
            WCSession *aDefaultSession           = [WCSession defaultSession];
            aDefaultSession.delegate  = self;
            if([aDefaultSession isPaired] && [aDefaultSession isWatchAppInstalled]){
                [aDefaultSession activateSession];
                [aDefaultSession transferUserInfo:infoToSend];
            }
        }
    }

使用updateApplicationContext:错误:

在这种情况下,设备将在激活时发送最新的上下文以进行监视.这意味着可以说,如果您已发送了三个信息,那么当监视"被激活时,它将仅接收最新的一个信息,而不会收到委托方法-(void)session:(WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext中的前一个信息.

In this case, Device will send the latest Context to Watch on activation. That means let say If you have sent three Info back to back then When Watch is Activated it will receive only latest one, not previous ones in delegate method - -(void)session:(WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext.

-(void)sendDictionaryToWatch:(NSDictionary*)infoToSend{
    if([WCSession isSupported]){
        WCSession *aDefaultSession           = [WCSession defaultSession];
        aDefaultSession.delegate  = self;
        if([aDefaultSession isPaired] && [aDefaultSession isWatchAppInstalled]){
            [aDefaultSession activateSession];
            [aDefaultSession updateApplicationContext:infoToSend error:nil];
        }
    }
}

这篇关于使用Watch连接框架将数据从iPhone传输到iWatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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