How to remain getting the location continuously in the background in ios8? - ios

How to remain getting the location continuously in the background in iOS8 and you must try your best to save electricity? Can anybody give me some advice?

In your project setting, Select Target and go to Capabilities, turn ON the background mode and tick the location updates and background fetch.
This will add background mode in your project plist.
Now, to get continuous location updates even in background, add this code in AppDelegate's applicationDidEnterBackground: method. This code will kill the background task every time and restart it. So, even when app is in background, you'll receive background location updates.
- (void)applicationDidEnterBackground:(UIApplication *)application {
if ([[UIDevice currentDevice] respondsToSelector:#selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E 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
}];
}
}
}
Now to extend device battery life, you can use locationManager:didUpdateLocations: method as it is only called when location changes according to desired accuracy.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
if (location != nil) {
strLatitude = [NSString stringWithFormat:#"%f", location.coordinate.latitude];
strLongitude = [NSString stringWithFormat:#"%f", location.coordinate.longitude];
}
}

Found some Solution here
Study the example from Ray Wenderlich. The sample code works perfectly.
You can add timer to it by using this code snippet this may reduce battery consumption a bit :
-(void)applicationDidEnterBackground {
[self.locationManager stopUpdatingLocation];
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
self.timer = [NSTimer scheduledTimerWithTimeInterval:intervalBackgroundUpdate
target:self.locationManager
selector:#selector(startUpdatingLocation)
userInfo:nil
repeats:YES];
}

Related

iOS8 UILocalNotification not working

I am using following code for local notification. But It is not working. Location is successfully being updated and it get into these methods but notification is not being fired. Any idea?:
NOTE: It is working when the app is in background but not working when the app is closed.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Override point for customization after application launch.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
return YES;
}
-(void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
if ([region isKindOfClass:[CLBeaconRegion class]]) {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"You are checked in";
notification.soundName = #"Default";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
}
You should keep in mind that notifications present only when your app in background, not at the foreground. If your are in foreground implement - application:didReceiveLocalNotification: of the AppDelegate and handle notification manually by yourself.
UPD
If your app is not running even in background, your code will not be executed. Look for Background modes (Tracking the User’s Location section) for possible solutions in order to ask system launch your app by events even currently it is not in the memory
My case was the notification was disabled from the
Settings -> Notifications -> YOUR_APP -> Allow Notifications
Local notification will work even your application removed from the background. But in your case, you are listening to the location manager event and triggering the local notification inside the delegate method. Your location manager event will not get triggered once you killed the application. So your local notification will not get triggered at all.
Wen app is closed applicationDidEnterBackground was called so put this background task code. and class the local notification.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
//// just u call the method want ever u want example
[self notification];
}
- (void)notification
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"You are checked in";
notification.soundName = #"Default";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
I think this is helpful to u.
When your app gets killed, beacon monitoring will still relaunch it — that's the great thing about beacon monitoring!
All you need to do is re-start the monitoring in the application:didFinishLaunchingWithOptions: method. If you do that, the didEnterRegion event which resulted in launch of the app will immediately get delivered to the delegate, which should trigger the notification.
Estimote has a more detailed guide:
Launching notifications in iOS when the app is killed. It uses ESTBeaconManager, ESTBeaconManagerDelegate and ESTBeaconRegion classes from the Estimote SDK, but you can simply replace these with regular Core Location classes for the same effect.

End background task and wait until app becomes active again - BLE processing data

my app downloads a bunch of data through BLE from a peripheral. If I lock the screen my app gets moved into the background and it starts an background task. The download finishes fine but if the processing (which takes rather long because it is a lot of data) begins the app craches because it cant connect to the database.
I want to stop the execution at that point and wait for the app to become active again, but somehow I cant achieve this. I think I need some kind of semaphore to wait for the app to become active.
Here my code so far:
- (void)viewDidLoad
{
//Some other code
//initialize flag
isInBackgroud = NO;
// check if app is in the background
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
// check if app is in the foreground
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appDidEnterForeground) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)appDidEnterBackground {
NSLog(#"appDidEnterBackground");
isInBackground = YES;
UIApplication *app = [UIApplication sharedApplication];
NSLog(#"remaining Time: %f", [app backgroundTimeRemaining]);
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"expirationHandler");
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
- (void)appDidEnterForeground {
NSLog(#"appDidEnterForeground");
isInBackground = NO;
if (bgTask != UIBackgroundTaskInvalid) {
UIApplication *app = [UIApplication sharedApplication];
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}
//BLE connection and reading data via notification
//when finished [self processData] is called.
- (void)processData {
if (isInBackground) {
//set reminder
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = [NSString stringWithFormat:#"Data was downloaded, return to the application to proceed processing your data."];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
UIApplication *app = [UIApplication sharedApplication];
//end background task
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
//wait for application to become active again
while (isInBackground) {
NSLog(#"isInBackground");
NSLog(#"remaining Time: %f", [app backgroundTimeRemaining]);
sleep(1);
}
//process data
}
So I have notices that if I call [app endBackgroundTask:bgTask]; the app just continues running but then crashes when I want connect to my database. Thats why I added the while(isInBackground) loop. I know that this is not good practice because it actively wastes CPU time while doing noting. I should use a semaphore at that point, but I cant figure out how to do it.
Because I'm actively waining in that loop, appDidEnterForegronund is never called and the loop runs forever.
You shouldn't be looping because your app only gets so long to process before it's stopped by iOS. Instead, when your app enters the background, set a state variable that it's in the background. Do the same for the foreground.
Only update the database if you're in the foreground, otherwise, set a state variable that tells your app that you've finished downloading, but still need to process the data. Store the data if you need to.
Then, when your app is relaunched, check the state of that variable and do the processing.
Instead of sitting in a loop waiting for some state to change, set variables, and use event-driven programming.

MKDirections calculate directions when app is in the background

I want to calculate directions to a destination when my app is in the background every time it receives a location update.
However, the [MKDirections calculateDirectionsWithCompletionHandler] is an asynchronous call, and my question is therefore: will my app be terminated if this takes more than 5 seconds to complete after my app has received a location update? What would be your recommendations to make sure this request has enough time to finish?
In order to request extra time for your app to run in the background to complete its tasks (asynchronous or not), you can use beginBackgroundTaskWithExpirationHandler:.
For example:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// If the application is in the background...
if([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
// Create the beginBackgroundTaskWithExpirationHandler
// which will execute once your task is complete
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:application forKey:#"application"];
// Start a timer to keep the program alive for 180 seconds max
// (less if that's all that's necessary)
[NSTimer scheduledTimerWithTimeInterval:180 target:self selector:#selector(delayMethod:) userInfo:nil repeats:NO];
});
}
// ... the rest of your didUpdateToLocation: code ...
}
- (void)delayMethod:(NSTimer*)timer {
// End the background task you created with beginBackgroundTaskWithExpirationHandler
[[[timer userInfo] objectForKey:#"application"] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}

Start background task after receiving push in Suspended mode

It may seem that this question was asked several times, but I'm facing a weird problem.
I have server configured to send push notification with content-available = 1 flag.
I have configured my app to work in background Background Modes on for Location Update, Background fetch and Remote Notifications.
Also I have implemented all necessary code to receive push notifications in background and to start background task.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
__block UIBackgroundTaskIdentifier bg_task = background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
//Clean up code. Tell the system that we are done.
[application endBackgroundTask: bg_task];
bg_task = UIBackgroundTaskInvalid;
}];
//### background task starts
[self updateLocationToServer];
//#### background task ends
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)updateLocationToServer{
[locationManager updateLocationWithCompletionHandler:^(CLLocation *location, NSError *error, BOOL locationServicesDisabled) {
if (error)
{
// Handle error here
if (locationServicesDisabled) {
// Location services are disabled, you can ask the user to enable them for example
}
}
else
{
// Do whatever you want with the current user's location
NSString *deviceID = [userDefs objectForKey:#"deviceID"];
isConnected = [[userDefs objectForKey:#"connected"] boolValue];
if (isConnected) {
if (deviceID) {
[self sendLocation:deviceID];
}
}
localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.1];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:#"Lat: %# Long:%#",[NSNumber numberWithFloat:location.coordinate.latitude],[NSNumber numberWithFloat:location.coordinate.longitude]];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"Lat: %# Long:%#",[NSNumber numberWithFloat:location.coordinate.latitude],[NSNumber numberWithFloat:location.coordinate.longitude]);
//Clean up code. Tell the system that we are done.
[[UIApplication sharedApplication] endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;}}];}
EDIT: Added code where I end background task. background_task variable is global.
The app receives push in background normally until it goes to suspended mode. The problem is that, after background task ends, and the app goes to suspended mode it does not run the code again when it receives push notification but didReceiveRemoteNotification: fetchCompletionHandler: does not get called. But when I open the app and exit with home button it will work again within "that" 3 minutes until it goes to suspended mode.

How to properly use location in background - app got rejected 3 times

My app got rejected by Apple three times, all with the same rejection letter, which is:
We found that your app uses a background mode but does not include
functionality that requires that mode to run persistently. This
behavior is not in compliance with the App Store Review Guidelines.
We noticed your app declares support for location in the
UIBackgroundModes key in your Info.plist but does not include features
that require persistent location.
It would be appropriate to add features that require location updates
while the app is in the background or remove the "location" setting
from the UIBackgroundModes key.
If you choose to add features that use the Location Background Mode,
please include the following battery use disclaimer in your
Application Description:
"Continued use of GPS running in the background can dramatically
decrease battery life."
For information on background modes, please refer to the section
"Executing Code in the Background" in the iOS Reference Library.
Now, as far as I know I am running on the background and "doing something"...
In my AppDelegate I have the following code in didFinishLaunchingWithOptions:
if ([[launchOptions allKeys] containsObject:UIApplicationLaunchOptionsLocationKey] &&
([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]))
{
id locationInBackground = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
if ([locationInBackground isKindOfClass:[CLLocation class]])
{
[self updateMyLocationToServer:locationInBackground];
}
else
{
//Keep updating location if significant changes
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
self.bgLocationManager = locationManager;
self.bgLocationManager.delegate = self;
self.bgLocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[bgLocationManager startMonitoringSignificantLocationChanges];
}
}
The AppDelegate also starts a location manager and makes himself the delegate.
Then, I have the following code for handling the location updates on the background:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self updateMyLocationToServer:newLocation];
}
-(void)updateMyLocationToServer:(CLLocation*)myNewLocation
{
// NSLog(#"Updating Location from the background");
NSString *fbID = [NSString stringWithString:[facebookDetails objectForKey:#"fbID"]];
NSString *firstName = [NSString stringWithString:[facebookDetails objectForKey:#"firstName"]];
NSString *lastName = [NSString stringWithString:[facebookDetails objectForKey:#"lastName"]];
NSString *urlString = [NSString stringWithFormat:#"MY_SERVER_API", fbID, myNewLocation.coordinate.latitude, myNewLocation.coordinate.longitude, firstName, lastName];
NSURL *url = [NSURL URLWithString:urlString];
__block ASIHTTPRequest *newRequest = [ASIHTTPRequest requestWithURL:url];
[newRequest setCompletionBlock:^{
}];
[newRequest setFailedBlock:^{
}];
// [newRequest setDelegate:self];
[newRequest startAsynchronous];
}
I also put a disclaimer in my app description page:
Intensive use of GPS running in the background can dramatically decrease battery life. For this reason, MY_APP_NAME runs on the background just listening for significant location changes.
Is there anything I'm missing here?
This question is old and already answered but you dont need the UIBackgroundModes key if you collect locations using the startMonitoringSignificantLocationChanges API
In locationManager:didUpdateToLocation:fromLocation: or in updateMyLocationToServer: You should check if application is in background state by eg.
[UIApplication sharedApplication].applicationState == UIApplicationStateBackground
And then if Your app is in background mode You should use eg.
backgroundTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:
^{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
}];
/*Write Your internet request code here*/
if (bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
bgTask = UIBackgroundTaskInvalid;
}
This way application should perform this task completely.
startMonitoringSignificantLocationChanges don't require a background mode registration. Only continuous location changes do.

Resources