2

I am using the watch to send a message to the iPhone with the sendMessage function. On the iPhone side, I am replying with the didReceiveMessage function.On the iPhone side I am running an HTTP request based on the message sent to it and replying with the reply from the request. It works by a button being pressed on the watch. The first time I press the button, no reply ever reaches the watch but on the second try and every try after I will always get a response from the iPhone. Does anyone know why the first try will never work? Keep in mind that the first try will work if I have the watch app and the iOS app open at the same time. (In the other cases I only have the watch app open)

John Smith
  • 123
  • 4
  • Are you sending the HTTP response within the reply handler? If you are doing asynchronous HTTP call you could send a message to the Watch then the HTTP call is finished – Philip Jul 20 '15 at 13:54
  • I am sending the HTTP response within the reply handler – John Smith Jul 20 '15 at 13:56
  • Have you considered changing it so that you send a message back to the watch when the HTTP request is finished instead? This will erase bugs cause by laggy 3g etc. However, WC InstantMessaging should work even if the iphone app isn't running (the first time aswell). Try removing the HTTP request and see if you get a response the first time. – Philip Jul 20 '15 at 13:59
  • If I remove the HTTP request it will respond on the first try. How would I go about sending the message when the HTTP request is finished? – John Smith Jul 20 '15 at 14:01

1 Answers1

0

You need to call the HTTP request asynchronously and send the message back to the watch within the completion handler of the HTTP request.

This will code example will send a async request Link:

NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{


if (error)
                        {
                            //NSLog(@"Error,%@", [error localizedDescription]);
                        }
                        else 
                        {
                            //This is where you send message to the watch
                        } 
}];
Community
  • 1
  • 1
Philip
  • 2,287
  • 1
  • 22
  • 37
  • It still did not work on the first try. Any other suggestions as to what the issue may be? On the iPhone side, I have all the session code being run in the appdelgate file, so there should not be an issue of the app not being available. – John Smith Jul 20 '15 at 14:17
  • I am using AFNetworking so it is already asynchronous calls for request – John Smith Jul 20 '15 at 14:43