Data transfering from iPhone to iWatch with Watch connectivity framework [closed]

六月ゝ 毕业季﹏ 提交于 2019-12-08 07:07:04

问题


I want to ask a question about watch connectivity.

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) Is it possible to create login screen(to get user input from text fields) on iWatch

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

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

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


回答1:


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

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) Is it possible to create login screen(to get user input from text fields) on iWatch

NO, Text fields are not available in WatchOS2.

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

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

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) Is it possible to read iPhone app's database(like sql lite db on iPhone app) from iWatch application

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) How to transfer dictionary from iPhone app to iWatch app. Share any example plz.

There are Two ways to perform these things:

Using TransferUserInfo

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];
            }
        }
    }

Using updateApplicationContext:error:

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];
        }
    }
}


来源:https://stackoverflow.com/questions/36617639/data-transfering-from-iphone-to-iwatch-with-watch-connectivity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!