objective c - iOS / Apple Watch: iPhone app network request callback blocks not triggered when app is in background -
my apple watch app sends message companion iphone app. in main app's handlewatchkitextensionrequest, send request server:
- (void)application:(uiapplication *)application handlewatchkitextensionrequest:(nsdictionary *)userinfo reply:(void (^)(nsdictionary *))reply { if ([[userinfo objectforkey:@"request"] isequaltostring:@"getpendingchallenge"]) { [myclient getpendingnotifications:someid withdomain:host withsuccessblock:^(id responseobject) { // process responseobject ... reply(response); return; } withfailureblock:^(nserror *error, nsstring *responsestring) { // error handling return; }]; } } getpendingnotifications above regular network request using afnetworking.
it works when app active. because network request used populate ui on apple watch, not wish main app active. however, when main app on iphone in background, can see network request being sent out, withsuccessblock or withfailureblock callback blocks in above code never gets triggered.
can phone app receive network request responses in background mode? if so, doing wrong?
i have found solution online works me, post (http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply) brian gilham.
and here's code works me.
- (void)application:(uiapplication *)application handlewatchkitextensionrequest:(nsdictionary *)userinfo reply:(void (^)(nsdictionary *))reply { // there chance ios app gets killed if it's in background // before has chance reply apple watch. // solution have app respond request asap, complete other tasks. // following code begins – , ends, after 2 seconds – empty background task right @ beginning of delegate method // kick off background task real work // more details see http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird-trick-to-fix-openparentapplicationreply __block uibackgroundtaskidentifier bogusworkaroundtask; bogusworkaroundtask = [[uiapplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{ [[uiapplication sharedapplication] endbackgroundtask:bogusworkaroundtask]; }]; dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(2 * nsec_per_sec)), dispatch_get_main_queue(), ^{ [[uiapplication sharedapplication] endbackgroundtask:bogusworkaroundtask]; }); __block uibackgroundtaskidentifier realbackgroundtask; realbackgroundtask = [[uiapplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{ reply(nil); [[uiapplication sharedapplication] endbackgroundtask:realbackgroundtask]; }]; if ([[userinfo objectforkey:@"request"] isequaltostring:@"getpendingchallenge"]) { [self handlewatchkitgetpendingchallengerequest:reply]; } [[uiapplication sharedapplication] endbackgroundtask:realbackgroundtask]; } - (void)handlewatchkitgetpendingchallengerequest:(void (^)(nsdictionary *))reply { ... [myclient getpendingnotifications:someid withdomain:host withsuccessblock:^(id responseobject) { // process responseobject reply(response); return; } withfailureblock:^(nserror *error, nsstring *responsestring) { // error handling reply(nil); return; }]; }
Comments
Post a Comment