在iOS设备上接收观看操作系统消息

时间:2016-01-15 13:21:14

标签: ios objective-c watchkit watch-os-2

大家好,感谢你的支持,

我刚刚开始使用Watch OS 2和Objective-C,我正试图在点击按钮时向iPhone设备发送消息,我不知道下一种方法是否是最好的,但是来自Apple docs it似乎最能满足我的需求,因为我需要发送请求并配对iOS设备并接收一些用户信息,我还读到这只适用于前景中的应用程序是一个缺点:(

更新1

- (IBAction)didTappedButton {

        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];

        if ([session isReachable] == YES) {

            NSDictionary *postDictionarry = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObject:@"retrieveAPISessionKey"] forKeys:[NSArray arrayWithObject:@"request"]];

            [self.button setBackgroundColor:[UIColor blueColor]];

            [session sendMessage:postDictionarry
                    replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
                        [self postToServer];
                    }
                    errorHandler:^(NSError * _Nonnull error) {
                         [self.button setBackgroundColor:[UIColor redColor]];
                        [self showAlertViewwithTitle:@"Oops..." andMessage:@"Something went Wrong"];

                    }];
        }else{

            [self showAlertViewwithTitle:@"Oops..." andMessage:@"Please pair with a device"];

        }

}

在AppDelegate中我实现了.h中的下一个代码:

@import WatchConnectivity;

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIGestureRecognizerDelegate, WCSessionDelegate>

和.m:

- (void)session:(nonnull WCSession *)session
didReceiveMessage:(NSDictionary<NSString *,id> *)message
   replyHandler:(void(^)(NSDictionary<NSString *,id> *))replyHandler {


    NSString *action = message[@"request"];
    NSString *actionPerformed;
    // more code here...

}

注意我在模拟器上测试并且在获取轻敲手势方面存在问题,而且我在手表模拟器上看到很多微调器

更新2

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    // Configure interface objects here.
    WCSession *session = [WCSession defaultSession];
    session.delegate = self;
    [session activateSession];

}

- (IBAction)didTappedButton {



        if ([[WCSession defaultSession] isReachable] == YES) {

            NSDictionary *postDictionarry = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObject:@"retrieveAPISessionKey"] forKeys:[NSArray arrayWithObject:@"request"]];

            [self.button setBackgroundColor:[UIColor blueColor]];

            [[WCSession defaultSession] sendMessage:postDictionarry
                    replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
                        [self postData];
                    }
                    errorHandler:^(NSError * _Nonnull error) {
                         [self.button setBackgroundColor:[UIColor redColor]];
                        [self showAlertViewwithTitle:@"Oops..." andMessage:@"Something went Wrong"];

                    }];
        }else{

            [self showAlertViewwithTitle:@"Oops..." andMessage:@"Please pair with a device"];

        }

}

- (void)postData{

    //post stress signal to server

}


- (void)showAlertViewwithTitle:(NSString *)title andMessage:(NSString *)message{

    WKAlertAction *act = [WKAlertAction actionWithTitle:@"OK" style:WKAlertActionStyleCancel handler:^(void){}];
    NSArray *actions = @[act];
    [self presentAlertControllerWithTitle:title message:message preferredStyle:WKAlertControllerStyleAlert actions:actions];

}

所以,只是成功发送消息并配对设备,但现在我没有在iOS应用程序中收到发送的字典。

1 个答案:

答案 0 :(得分:0)

您需要在WatchKit App Extension和iPhone应用程序中激活WCSession。根据您向我们展示的代码,您似乎没有在iPhone应用程序中激活它。

将以下内容添加到iPhone应用程序中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

if ([WCSession isSupported]) {
    WCSession* session = [WCSession defaultSession];
    session.delegate = self;
    [session activateSession];
}
...

然后,您应该可以通过现有的session:didReceiveMessage:replyHandler:方法接收消息。

相关问题