Anupam Mishra
Anupam Mishra

Reputation: 3588

is it proper to make Direct api call from apple watch extension without help of parent application

In my Apple watch application, I was making API call by the help of parent application in this way from InterfaceController

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];    
    [dictionary setObject:@"getSomething" forKey:@"action"];        
    [MainInterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) {
        NSLog(@"Reply received by Watch app: %@", replyInfo); // the reply from the appDelegate... 
         }

& in parent application AppDelegate.m to return response data

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
       NSLog(@"Request received by iOS app");
if( [userInfo objectForKey:@"action"] isEqualToString:@"getSomething"] ){
//call you're Web API
//send the reponse to you're glance :    
reply(DictResponse);// some Dictionary from your web API... 

}

it seems that watch app always open the parent app in background OR foreground to get response. Now i am looking the functionality a bit different as Apple Suggested

I am taking the required information from sharedUserDefaults this way

self.sharedUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.xxxxxxxx"];

& making the direct API call from watch extension which is working proper like this -

NSMutableURLRequest *request=[NSMutableURLRequest  requestWithURL:[NSURL URLWithString:url]                                                         cachePolicy:NSURLRequestUseProtocolCachePolicy                                                      timeoutInterval:20.0];
    [request setHTTPMethod:@"GET"];
    [self sendURLRequestWithURLString:request];

-(void)sendURLRequestWithURLString:(NSMutableURLRequest *)request
{
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (!connectionError)
         {
             if (data)
             {
                 id jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                 if ([jsonObj isKindOfClass:[NSDictionary class]])
                 {
                     NSDictionary *jsonDict = (NSDictionary*)jsonObj;
                     NSDictionary *featuredEvent = [jsonDict valueForKey:@"featured_event"];

                  self.completionHandler([featuredEvent valueForKey:@"events"],[[featuredEvent valueForKey:@"total"] integerValue],[featuredEvent valueForKey:@"error_msg"]);

                 }
                 else
                 {

                     // self.completionHandler(false);

                 }
             }
             else
             {
                 //self.completionHandler(false);
             }
         }
         else
         {
             [self sendURLRequestWithURLString:request];
             //self.completionHandler(false);
         }
     }];

}

I wanted to know is it safe & proper way to call API from watch extension.

Upvotes: 0

Views: 2928

Answers (1)

Jannik Michel
Jannik Michel

Reputation: 366

It is perfectly valid to use the NSUserDefaults for sharing data between your iOS-App and your WatchKit-Extension. And as long as it is possible (from language standpoint), you can legitimately call your API directly from the Watch Kit Extension.

Note:
As Apple's Programming Guidelines for Apple Watch state, the main iOS-App has fewer restrictions, e.g. you cannot run a watch app in background mode. So for that or similar use cases, and also for resource-intensive code, you should rely on the openParentApplication-Call.

Upvotes: 0

Related Questions