ios - Location Services not working in background called by WatchKit -
my apple watch app requires data , requests corresponding iphone app. fulfill request iphone app requires users location. after receiving , testing real apple watch found out iphone app not receive location updates when running in background. if iphone app active in foreground works without issues. simulator worked in both cases.
in both cases (active , background) watchkit extension calls , starts iphone app , goes way until startupdatinglocation called in iphone app. in case app running in background didupdatelocations never called.
i tried requestalwaysauthorization requestwheninuseauthorization. no difference. activated "location updates" background mode within capabilities. again no difference.
has else faced same problem , found way receive location in background?
here code. first check if authorization required.
// ios 8 check avoid crash on older ios if ([self.locationmanager respondstoselector:@selector(requestwheninuseauthorization)]) { [self requestlocationalwaysauthorization]; } else { [self runlocationupdate]; } here check proper location manager rights.
- (void)requestlocationalwaysauthorization { clauthorizationstatus currentauthstatus = [cllocationmanager authorizationstatus]; if (currentauthstatus == kclauthorizationstatusdenied) { //request user change setting } else if (currentauthstatus == kclauthorizationstatusrestricted) { //request user change setting } else if (currentauthstatus == kclauthorizationstatusnotdetermined) { [self.locationmanager requestalwaysauthorization]; [self runlocationupdate]; } else if (currentauthstatus == kclauthorizationstatusauthorizedwheninuse) { //maybe when in use enough? [self runlocationupdate]; } else if (currentauthstatus == kclauthorizationstatusauthorizedalways) { //all ok [self runlocationupdate]; } }
here call of startupdatinglocation. didupdatelocations delegate called when iphone app active.
-(void)runlocationupdate { [self.locationmanager startupdatinglocation]; }
three things check , aware of:
location permissions
[self.locationmanager requestalwaysauthorization];acknowledged once os. if have requested permission, doesn't matter level, os not display request user. os pass on request , leave permission level is. time can assured os display request user if[cllocationmanager authorizationstatus]returnskclauthorizationstatusnotdetermined. in every other case, must manually request permission displaying alert or other form of ui display. note os retains whether or not displayed request, if delete app , reinstall it. test, need reset simulator's content or iphone's location privacy.make sure have added plist keys
nslocationalwaysusagedescription,nslocationwheninuseusagedescriptionif don't add plist, os ignore location permission requests.if want use
requestalwaysauthorizationlocation data phone (not watch app extension) while phone app in background, require register background modes location updates under project>target>capabilities.
update use background task give app time respond when in background. this:
-(void)application:(uiapplication *)application handlewatchkitextensionrequest:(nsdictionary *)userinfo reply:(void (^)(nsdictionary *replyinfo))reply{ uiapplication *app = [uiapplication sharedapplication]; uibackgroundtaskidentifier bgtask __block = [app beginbackgroundtaskwithname:@"watchapprequest" expirationhandler:^{ [[uiapplication sharedapplication] endbackgroundtask:bgtask]; bgtask = uibackgroundtaskinvalid; }]; //make calls here tasks, when finished, send reply terminate background task //send reply watch reply(replyinfo); dispatch_after(dispatch_time(dispatch_time_now, 2.0 * nsec_per_sec), dispatch_get_main_queue(), ^{ [app endbackgroundtask:bgtask]; bgtask=uibackgroundtaskinvalid; }); }
Comments
Post a Comment