在WatchKit中不会调用func session(..)

时间:2015-07-09 07:34:45

标签: objective-c swift watchkit

我正在尝试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")
}

1 个答案:

答案 0 :(得分:1)

您要查找的委托方法是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;