gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:43:22 24 4
gpt4 key购买 nike

我有一个 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 在 vi​​ewDidLoad 方法中声明,它包含数据。

在接收端(Watch Extension)我像这样配置 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),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35274391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com