iOS app background location access using a timer -


i looking solution access/stop location services while app in background. app takes continuous location when it's sent background (it has access continuous location) . it's necessary app functionality.

so know few things:

  • how long app can take continuous location while it's still in background? (before os kills background process or that)

  • if want add timer after 60 minutes app stop taking location, correct approach?

background location updation can done using following code:

in appdelegate class:

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {  if ([launchoptions objectforkey:uiapplicationlaunchoptionslocationkey]) {          // "afterresume" flag show receiving location updates         // key "uiapplicationlaunchoptionslocationkey"         self.sharemodel.afterresume = yes;          [self.sharemodel startmonitoringlocation];     }      return yes; } - (void)applicationdidenterbackground:(uiapplication *)application {  [self.sharemodel stopcontinuoslocationupdate];     [self.sharemodel restartmonitoringlocation]; } - (void)applicationdidbecomeactive:(uiapplication *)application {  //remove "afterresume" flag after app active again.     self.sharemodel.afterresume = no;      [self.sharemodel startcontinuoslocationupdate]; } 

in location update class, locationmanager.m:

#import <corelocation/corelocation.h>  @property (nonatomic) cllocationmanager * anotherlocationmanager;  - (void)startcontinuoslocationupdate {     clauthorizationstatus status = [cllocationmanager authorizationstatus];      if (status == kclauthorizationstatusdenied)     {         nslog(@"location services disabled in settings.");     }     else     {         // ios 8         if ([self.anotherlocationmanager respondstoselector:@selector(requestalwaysauthorization)])         {             [self.anotherlocationmanager requestalwaysauthorization];         }         // ios 9         if ([self.anotherlocationmanager respondstoselector:@selector(setallowsbackgroundlocationupdates:)])         {             [self.anotherlocationmanager setallowsbackgroundlocationupdates:yes];         }          [self.anotherlocationmanager startupdatinglocation];     } }  - (void)stopcontinuoslocationupdate {     [self.anotherlocationmanager stopupdatinglocation]; }  - (void)startmonitoringlocation {     if (_anotherlocationmanager)         [_anotherlocationmanager stopmonitoringsignificantlocationchanges];      self.anotherlocationmanager = [[cllocationmanager alloc]init];     _anotherlocationmanager.delegate = self;     _anotherlocationmanager.desiredaccuracy = kcllocationaccuracybestfornavigation;     _anotherlocationmanager.activitytype = clactivitytypeothernavigation;      if(system_version_greater_than_or_equal_to(@"9.0")) {         [_anotherlocationmanager setallowsbackgroundlocationupdates:yes];     }     else if(system_version_greater_than_or_equal_to(@"8.0")) {         [_anotherlocationmanager requestalwaysauthorization];     }     [_anotherlocationmanager startmonitoringsignificantlocationchanges]; }  - (void)restartmonitoringlocation {     [_anotherlocationmanager stopmonitoringsignificantlocationchanges];      if(system_version_greater_than_or_equal_to(@"9.0")) {         [_anotherlocationmanager setallowsbackgroundlocationupdates:yes];     }     else if(system_version_greater_than_or_equal_to(@"8.0")) {         [_anotherlocationmanager requestalwaysauthorization];     }     [_anotherlocationmanager startmonitoringsignificantlocationchanges]; }   #pragma mark - cllocationmanager delegate  - (void)locationmanager:(cllocationmanager *)manager didupdatelocations:(nsarray *)locations {     if(_dictlocation && [_dictlocation iskindofclass:[nsdictionary class]])     {         float latitudevalue = [[rvcommon validatedatafornumber:_dictlocation[@"lat"]] floatvalue];         float longitudevalue = [[rvcommon validatedatafornumber:_dictlocation[@"lng"]] floatvalue];         cllocation *facilitylocation = [[cllocation alloc] initwithlatitude:latitudevalue longitude:longitudevalue];         cllocation *mostrecentlocation = locations.lastobject;          cllocationdistance distanceinmeters = [mostrecentlocation distancefromlocation:facilitylocation];         if (distanceinmeters <= 500.0)         {             //here informing server when user within 500mts of coordinate.          }     }      nslog(@"locationmanager didupdatelocations: %@",locations); } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -