Getting space for an iOS application when it is in the background and even killed

My application should get the user's location when the application is active and when it is inactive and killed. When the user's location is near the store, the application should send a local notification.

I'm not sure what exactly is happening, but I can not get my application to find a place in the background and wakes up when it is killed.

I have a location manager (singleton used for both cases whenInUse and Always) and I have both NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription defined in .plist

What am I doing:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //The app has been killed/terminated (not in background) by iOS or the user. if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]){ _locationManager = [CoreLocationManager sharedInstance]; _locationManager.isAppActive = NO; _locationManager.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; _locationManager.locationManager.activityType = CLActivityTypeOtherNavigation; if ([_locationManager.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [_locationManager.locationManager requestAlwaysAuthorization]; } [_locationManager addLocationManagerDelegate:self]; } } - (void)applicationDidBecomeActive:(UIApplication *)application { if (_locationManager.locationManager){ _locationManager.isAppActive = YES; [_locationManager.locationManager stopMonitoringSignificantLocationChanges]; } _locationManager = [CoreLocationManager sharedInstance]; if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [_locationManager.locationManager requestAlwaysAuthorization]; } if ([_locationManager.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [_locationManager.locationManager requestWhenInUseAuthorization]; } [_locationManager addLocationManagerDelegate:self]; [_locationManager.locationManager startUpdatingLocation]; } - (void)applicationDidEnterBackground:(UIApplication *)application { _locationManager.isAppActive = NO; if (_locationManager.locationManager){ [_locationManager.locationManager stopUpdatingLocation]; [_locationManager.locationManager stopMonitoringSignificantLocationChanges]; } if ([_locationManager.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [_locationManager.locationManager requestAlwaysAuthorization]; } [_locationManager.locationManager startMonitoringSignificantLocationChanges]; } 

Am I doing something wrong? I'm not sure it is strictly necessary to use geo lenses, but for the things that I read using startMonitoringSignificantLocationChanges, enough.

+5
ios objective-c core-location appdelegate
source share
1 answer

To get the location in the background, use the following code. This will make your application run in the background for a long time, restarting background tasks every time.

To use this, you must enable Background in Features in the project settings using Background Fetch and Location Updates .

 - (void)applicationDidEnterBackground:(UIApplication *)application { if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking IE iOS 4 if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance __block UIBackgroundTaskIdentifier background_task; //Create a task object background_task = [application beginBackgroundTaskWithExpirationHandler: ^{ [application endBackgroundTask:background_task]; //Tell the system that we are done with the tasks background_task = UIBackgroundTaskInvalid; //Set the task to be invalid //System will be shutting down the app at any point in time now }]; } } } 
+8
source share

All Articles