我正在尝试3天才能完成这项工作,但事实并非如此:/
我想向手表发送一些消息。但是函数会话不会被调用。
在我的父母中,我使用以下代码打开会话:
- (void)viewDidLoad {
[super viewDidLoad];
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
}
然后,如果要按下一个按钮,我想将字典发送到我的应用程序:
-(void)clickBackButton{
NSArray *obj = @[@"Mercedes-Benz SLK250", @"Mercedes-Benz E350"];
NSArray *key = @[[NSNumber numberWithInt:13], [NSNumber numberWithInt:22]];
NSDictionary *applicationDict = [NSDictionary dictionaryWithObject:obj forKey:key];
[[WCSession defaultSession] transferUserInfo:applicationDict];
}
在我的手表中,我将以下代码用于会话:
override func willActivate() {
super.willActivate()
if (WCSession.isSupported()) {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
而且这种方法永远不会被调用:
func session(session: WCSession, didFinishUserInfoTransfer userInfoTransfer: WCSessionUserInfoTransfer, error: NSError?) {
print("session called")
}
您正在寻找的委托方法是session:didReceiveUserInfo:
。如果您阅读了委托方法上面的评论,您将看到在接收方(您的手表)实现的是在发送方调用的,以确认传输已完成!
/** Called on the sending side after the user info transfer has successfully completed or failed with an error. Will be called on next launch if the sender was not running when the user info finished. */
- (void)session:(WCSession * __nonnull)session didFinishUserInfoTransfer:(WCSessionUserInfoTransfer *)userInfoTransfer error:(nullable NSError *)error;
/** Called on the delegate of the receiver. Will be called on startup if the user info finished transferring when the receiver was not running. */
- (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *, id> *)userInfo;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句