0

Hi everyone and thanks for your support in advance,

I just started working with Watch OS 2 and Objective-C and I am trying to send a message to an iPhone device when a button is tapped, I don't know if the next approach is the best but from Apple docs it seems to best feet my needs, as I need to send a request to and paired iOS device and receive some user info, I also read that this only works if the app in in foreground witch is a downside :(

Update 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"];

        }

}

And in the AppDelegate of I implemeted the next code in .h:

@import WatchConnectivity;

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

and in the .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...

}

note I am testing on the simulator and have an issues in getting tap gestures and also I see a lot of spinners on the watch simulator

Update 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];

}

So, just succeede in sending the message and also pair devices but now i don't receive the sent dictionary in the iOS app.

Laur Stefan
  • 1,519
  • 5
  • 23
  • 50
  • I'm sorry what was the question? //the code is generally correct, need more info for an answer //you might want to move the `[WCSession isSupported]` part, somewhere at initialization //also would be nice to see those "spinners" you mentioned – 4oby Jan 15 '16 at 14:14
  • @4boy shouldn't I keep the `[WCSession isSupported]` as I need it ti check – Laur Stefan Jan 15 '16 at 15:42
  • you need to check if supported only once, even more on watchOS 2 `isSupported` is always `YES` (: – 4oby Jan 15 '16 at 15:46
  • just updated the code \ – Laur Stefan Jan 15 '16 at 16:21
  • What about the objects in your message? I can't see what they are but even if you're sending custom objects in an NSArray they need to conform to NSCoding protocol and be converted to NSData. Maybe check on that – lzl Jan 15 '16 at 16:21
  • The current issue is that [session isReachable] return NO, any idea how this can happen (I tried with the ios app in foreground and also in background) – Laur Stefan Jan 15 '16 at 16:37
  • You can use sendApplicationContext for foreground, I'm not sure about the isReachable issue, sorry, I assume you have your watch paired and the bluetooth is turned on in your iPhone – lzl Jan 15 '16 at 17:02
  • yes they are torn on and paired – Laur Stefan Jan 15 '16 at 17:04
  • If you're sending and not receiving, check my last message, the type of data youre sending is usually the issue. your dictionary has to be [String: AnyObject]... with AnyObject being NSNumber, NSDate, NSString, NSData, NSArray, NSDictionary. and if you have custom objects in NSArray or NSDictionary, convert them to NSData too. – lzl Jan 15 '16 at 17:14
  • the dictionary posted in the code above is what I am trying to do – Laur Stefan Jan 17 '16 at 12:43

1 Answers1

0

You need to activate your WCSession in both the WatchKit App Extension and in the iPhone application. Based on the code you've shown us, it does not appear you are activating it in your iPhone application.

Add the following to your iPhone application:

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

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

You should then be able to receive the message via your existing session:didReceiveMessage:replyHandler: method.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93