0

I would like to load some information in the WatchKit interface from a REST backoffice. Is there some fast way to execute a URL request passing by the host iOS app or I must concoct a specific protocol taking advantage of the bare WatchConnectivity functionalities?

Tamir Vered
  • 10,187
  • 5
  • 45
  • 57
Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75

4 Answers4

0

You should use NSURLSession directly from your WatchKit extension.

ccjensen
  • 4,578
  • 2
  • 23
  • 25
  • That is quite weird as the Watch lacks a sim card and so it is not able to connect to the internet by itself, but perhaps under Wifi coverage. I am looking for a way to submit an URL both under wifi and cellular coverage. I was considering using WCSession.sendMessage with an handler on which the main app would put the result after having performed an URLS request itself. Yet I am not clear whether that also works hen the main app is not active. – Fabrizio Bartolomucci Oct 18 '15 at 14:50
  • NSURLSession will take advantage of the phone's cellular if needed and available – ccjensen Feb 01 '16 at 07:19
0

NSURLSession works fine with Watchkit, but you will need to add Allows Arbitrary Loads on Watchkit Extension too if it is relevant.

Please see this post,

NSURLSession returns data as Null on Watch OS2 using Objective-C

Community
  • 1
  • 1
Karthik
  • 103
  • 7
0

I am using sendMessageData:replyHandler:, yet I have problems in having it properly working. The relative ticket is at: https://stackoverflow.com/questions/33211760/sendmessagedata-does-not-call-didreceivemessagedata-on-the-counterpart

Community
  • 1
  • 1
Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
0

Although Apple's example is in swift see https://developer.apple.com/videos/play/wwdc2015-228/?time=345 , I've pasted a snippet in obejctive-c utlisin the NSProcessInfo wrapper.

Apple's example also makes use of Semaphore's, should the extension be put to sleep, which I have omitted from below.

Following the below pattern enables you to hit a desired service directly from the watch.

NSProcessInfo *processInfo = [NSProcessInfo processInfo];

[processInfo performExpiringActivityWithReason:@"networkReq" usingBlock:^(BOOL expired) {
    if (!expired){            
        NSLog(@"we have an assertion");        
        NSTimeInterval secondsInThreeHours = 3 * 60 * 60;
        NSURL *forecast = [NSURL URLWithString:@"http::address"];

        NSURLSession *session = [NSURLSession sharedSession];
        [[session dataTaskWithURL:forecast
                completionHandler:^(NSData *data,
                                    NSURLResponse *response,
                                    NSError *error) {
                    // handle response
         }] resume];
    else{

        NSLog(@"Not background assertion or we are out of time");
    }
}];
MagicFlow
  • 477
  • 3
  • 17