簡體   English   中英

在iOS設備上接收Watch OS消息

[英]Receive Watch OS message on iOS device

大家好,感謝您的提前支持,

我剛剛開始使用Watch OS 2和Objective-C,並且試圖在按下按鈕時向iPhone設備發送消息,我不知道下一種方法是否最好,但是從Apple文檔看來,這似乎是最好的滿足我的需求,因為我需要向配對的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應用中沒有收到發送的字典。

您需要在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:方法接收消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM