ios – 数据未在真实设备上从iphone传输到iWatch(AppleWatch)

ios – 数据未在真实设备上从iphone传输到iWatch(AppleWatch),第1张

概述我有一个iOS应用程序,我在appleWatch上编写了一个扩展程序. 我正在使用transferUserInfo方法将数据(NSDictionary)发送到appleWatch扩展.一切都在模拟器中工作,但是当我试图在真实设备上运行应用程序时,似乎iWatch没有收到任何东西,虽然iPhone正在发送数据(我发现这是因为我调试了发送方). 我已经在两侧配置了WCSession.我已经在我应该接收 我有一个iOS应用程序,我在appleWatch上编写了一个扩展程序.
我正在使用transferUserInfo方法将数据(NSDictionary)发送到appleWatch扩展.一切都在模拟器中工作,但是当我试图在真实设备上运行应用程序时,似乎iWatch没有收到任何东西,虽然iPhone正在发送数据(我发现这是因为我调试了发送方).

我已经在两侧配置了WCSession.我已经在我应该接收数据的类中符合WCSessionDelegate.
我正在使用session:dIDReceiveUserInfo:方法来接收ExtensionDelegate中的数据但仍然像我说的一切在模拟器中工作正常但没有在真实设备上传输.

有没有人知道问题是什么?

这是代码:
在发件人方面:

在我的班级MensaVIEwController.m中

- (voID)vIEwDIDLoad   if ([WCSession isSupported]) {        session = [WCSession defaultSession];        session.delegate = self;        [session activateSession];   }   NSLog(@"A dish is being sent");  [session transferUserInfo:dishDictionary];}

dishDictionary在vIEwDIDLoad方法中声明,它包含数据.

在接收器端(观看扩展)
我配置WCSession并在ExtensionDelegate.m中接收数据,如下所示:

- (voID)applicationDIDBecomeActive {    if ([WCSession isSupported]) {        session = [WCSession defaultSession];        session.delegate = self;        [session activateSession];        NSLog(@"Session activated in iWatch");    }}

我有这个方法来接收数据:

- (voID)session:session dIDReceiveUserInfo:(NSDictionary<Nsstring *,ID> *)userInfo{    NSLog(@"Received data from the iPhone");    NSArray<Nsstring*> *dictionaryKeys = userInfo.allKeys;    for(int i = 0; i < dictionaryKeys.count; i++){        Boolean equalsMensaString = [dictionaryKeys[i] isEqualToString:@"beilagen"];        if(equalsMensaString)            [self handleTransferreddish:userInfo];        Boolean equalsNewsstring = [dictionaryKeys[i] isEqualToString:@"article"];        if(equalsNewsstring)            [self handleTransferrednews:userInfo];        Boolean equalsEventString = [dictionaryKeys[i] isEqualToString:@"description"];        if(equalsEventString)            [self handleTransferredEvents:userInfo];    }}
解决方法 如果您查看源函数说明,可以看到用于传输数据的方法的说明:

public func transferUserInfo(userInfo: [String : AnyObject]) -> WCSessionUserInfoTransfer

The system will enqueue the user info dictionary and transfer it to
the counterpart app at an opportune time. The transfer of user info
will continue after the sending app has exited. The counterpart app
will receive a delegate callback on next launch if the file has
successfully arrived. The userInfo dictionary can only accept the
property List types.

所以..当您实际使用该应用程序时,无法保证系统会发送此userInfo.

请改用以下方法:

public func sendMessage(message: [String : AnyObject],replyHandler: (([String : AnyObject]) -> VoID)?,errorHandler: ((NSError) -> VoID)?)

ClIEnts can use this method to send messages to the counterpart app. ClIEnts wishing to receive a reply to a particular
message should pass in a replyHandler block. If the message cannot be
sent or if the reply Could not be received,the errorHandler block
will be invoked with an error. If both a replyHandler and an
errorHandler are specifIEd,then exactly one of them will be invoked.
Messages can only be sent while the sending app is running. If the
sending app exits before the message is dispatched the send will fail.
If the counterpart app is not running the counterpart app will be
launched upon receiving the message (iOS counterpart app only). The
message dictionary can only accept the property List types.

并接收:

func session(session: WCSession,dIDReceiveMessage message: [String : AnyObject],replyHandler: ([String : AnyObject]) -> VoID)

Notice That:
This send can fail if the counterpart app is not open or the Session is not paired and active. So,in the case you can not miss any data you should use updateApplicationContext or transferUserInfo (I actually prefer updateApplicationContext)

session.sendMessage(messageData,replyHandler: { (replyData) -> VoID in            replyHandler?(replyData)            },errorHandler: { (error) -> VoID in                print("error: code:\(error.code) - \(error.localizedDescription)")                errorHandler?(error)                 do {                        try session.updateApplicationContext(messageData)                    } catch {                        print("There was an error trying to update watchkit app on the background")                    }        })

并确保通过正确实施来收到此案例

func session(session:WCSession,dIDReceiveApplicationContext applicationContext:[String:AnyObject])

总结

以上是内存溢出为你收集整理的ios – 数据未在真实设备上从iphone传输到iWatch(AppleWatch)全部内容,希望文章能够帮你解决ios – 数据未在真实设备上从iphone传输到iWatch(AppleWatch)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1048444.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存