Capture locations in all state apps

I am wondering how to capture the location when the application is not running, and save the database. Already after several textbooks, but no one worked. Please, before copying as a duplicate, I ask you to read the full question, since it cannot be.

I tried the following lessons:

Assuming I have no implementation for the capture location. I need every day at noon, grab the user's location and save the database, and then send the web service. This capture location must be executed regardless of whether the application is active in the background or not working (two rings on the middle button and drag).

I tried some tutorials found on the Internet, and even some offered by the community here, but still didn't work.

  • I added background modes - Location.
  • I allowed to receive the requestAlwaysAuthorization location.

The language can be in objective-c or fast, most importantly, I will learn how to capture this location when the application is in all states.

+4
source share
1 answer

OK, I'll make it as simple as possible ... Fancy background modes and choosing a background for the background, like this enter image description here

Follow these methods for every step.

When is TERMINATED Application

Appdelegate.h

  #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong,nonatomic) CLLocationManager *locationManager; @end 

AppDelegate.m

 #define userDef [NSUserDefaults standardUserDefaults] #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) #import "AppDelegate.h" #import <CoreLocation/CoreLocation.h> #import "AFNetworking.h" #import <GoogleMaps/GoogleMaps.h> @implementation AppDelegate{ BOOL fromTerminated; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { fromTerminated = NO; if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) { fromTerminated = YES; self.locationManager = [[CLLocationManager alloc]init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; self.locationManager.activityType = CLActivityTypeOtherNavigation; [self.locationManager startUpdatingLocation]; } return YES; } - (void)applicationDidBecomeActive:(UIApplication *)application { if(self.locationManager){ [self.locationManager stopMonitoringSignificantLocationChanges]; self.locationManager = nil; self.locationManager.delegate = nil; } self.locationManager = [[CLLocationManager alloc]init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; self.locationManager.activityType = CLActivityTypeOtherNavigation; if(IS_OS_8_OR_LATER) { [self.locationManager requestAlwaysAuthorization]; } [self.locationManager startMonitoringSignificantLocationChanges]; } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ NSLog(@"locationManager didUpdateLocations: %@",locations); if(fromTerminated){ CLLocation * newLocation = [locations lastObject]; CLLocationCoordinate2D theLocation = newLocation.coordinate; CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy; [userDef setObject:[NSString stringWithFormat:@"%f",theLocation.longitude] forKey:@"LONGITUDE"]; [userDef setObject:[NSString stringWithFormat:@"%f",theLocation.latitude] forKey:@"LATITUDE"]; self.myLocation = theLocation; self.myLocationAccuracy = theAccuracy; [self updateLocation]; } } -(void)updateLocation{ // call your webservice for updating } @end 

This code will do the following → Background fetch initiates a location change and starts the application, and didFInishLaunchingWithOptions will be called using the UIApplicationLaunchOptionsLocationKey in the parameter dictionary. If he finds this, it means the application is TERMINATED and woke up for background fetching. You now have 30 seconds or so to do your things. This way you create a location manager object and start the update, which will call your didUpdateLocations delegation didUpdateLocations , and then you can call your method to run this changed location on your server or database.

In your regular VC, create another Location Manager object, as you created in the didFinishiLaunchingWithOptions method, and implement the didUpdateLocation delegate didUpdateLocation , which will run until the application is in the foreground or in the background. Storing the application delegation method will launch the application when it is completed.

Greetings :)

[EDIT]

When the application is in the foreground or Background

 #import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> @property (nonatomic,strong) CLLocationManager *locationManager; @end @implementation ViewController{ } -(void)viewDidAppear:(BOOL)animated{ if(self.locationManager == nil){ _locationManager = [CLLocationManager new]; } _locationManager.delegate = self; _locationManager.distanceFilter = kCLDistanceFilterNone; _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) { [_locationManager requestAlwaysAuthorization]; } else { [_locationManager startUpdatingLocation]; } } - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { _locationManager = nil; CLLocation *location = [locations lastObject]; theUser.latitude = [NSString stringWithFormat:@"%f",location.coordinate.latitude]; theUser.longitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude]; } } - (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { switch (status) { case kCLAuthorizationStatusNotDetermined: { } break; case kCLAuthorizationStatusDenied: { } break; case kCLAuthorizationStatusAuthorizedWhenInUse: case kCLAuthorizationStatusAuthorizedAlways: { [_locationManager startUpdatingLocation]; //Will update location immediately } break; default: break; } } @end 

[EDIT]

To verify that the application starts after the state completes, make this change and click the start button and change the location of the device from the storyboard enter image description here

Make this change and run the project (do it in device debugging mode) and change the location to this and then insert a breakpoint in applicationDidFinishLaunchingWithOptions and you will see that the breakpoint has hit, which means the application was in a state of completion, but this change location caused the launch of the OS for the application. enter image description here

Hope could make you understand

+5
source

All Articles