I have done an app using location update as background mode, and I am updating my location after each keepAlive interval using webservice. I am using the below mentioned code.
if ([application respondsToSelector:#selector(setKeepAliveTimeout:handler:)])
{
[application setKeepAliveTimeout:120 handler:^{
DDLogVerbose(#"KeepAliveHandler");
// Do other keep alive stuff here.
}];
}
self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"ending background task");
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
self.backgroundTimerForLocationUpdate = [NSTimer scheduledTimerWithTimeInterval:120
target:self
selector:#selector(changeAccuracy)
userInfo:nil
repeats:YES];
and in the location delegate I am calling a web request for updating the location in server, to track the user.
Will apple reject the app for using the location update background mode.
Will apple reject the app for using the location update background mode.
According to the Apple Documentation you are only allowed to if your iOS App
Playing and Recording Background Audio An app that plays or records audio continuously (even while the app is running in the background) can register to perform those tasks in the background.Tracking the User’s Location and Implementing a VoIP Client (Voice over Internet Protocol). So Accordingly no problem at all but the final result will be declared by Apple Review Team
Good Luck
Apple will reject application if you start or stop location update while application is in background.
But you can get latest location update in background with method "startMonitoringSignificantLocationChanges". It will update current location value when app is in background and when you will come in foreground mode, You will have latest location information to perform any subsequence activity.
Related
I am developing an application (Family Safety) where one family member can request and get current location of other family member without consent of other family member . Even when the app is not running.
this cannot be achieved by push notifications as Push notifications won't work when app is not running. I don't want to show user a notification when someone is requesting location. I want it to work no matter app is running or not. Please suggest me any solution to achieve this task.
PS. please don't tell me that I shouldn't get user location like this or my app will be rejected. Because I am developing this application for Family Security company and family members will first read and accept to company's policy.
First use requestAlwaysAuthorization instead of requestWhenInUseAuthorization for CLAuthorizationStatus to access location when app is quite.
Now use
UIApplication* application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[self locationManager] startUpdatingLocation];//call you web-api here to update location on server.
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
});
This will help.
Requesting a location of a device without user intervention and that too, when the app is not running, is not possible in iOS.
The only way to get the Device location in iOS when the app is terminated, is to use Significant Location change. SLC will give location update even when the app is not running. It will just wake up the app and give the location update.
But if you want the location of the device on request, the app must be running to receive the request and process it.
If a locked iPhone detects an iBeacon signal registered with my app, can my app, during its ~5-10sec wake up time:
contact my web server to send some data?
send a predefined text message?
access the iPhone's current GPS location?
Yes, I can confirm you can contact a web server, and access fine location (GPS) as I commonly do this during the window you mention.
I have less experience sending SMS text messages on iOS, but my basic understanding is that you cannot send SMS messages at all in an automated fashion on iOS, you can only present the screen to the user to initiate the send. I do not believe you can do this when your app is not in the foreground.
You can, however, send local notifications that appear on the lock screen.
Yes app can do all these actions while phone is locked.
Also if you want to continue scanning and all these functionalities in the background you can use UIBackgroundTaskIdentifier
It will continue scanning in the background till there is any beacon detected. Once there will be no beacon available, it stops scanning in background at the end of tolerate time.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"=== DID ENTER BACKGROUND ===");
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"End of tolerate time. Application should be suspended now if we do not ask more 'tolerance'");
}];
if (bgTask == UIBackgroundTaskInvalid) {
NSLog(#"This application does not support background mode");
}
else {
//if application supports background mode, we'll see this log.
NSLog(#"Application will continue to run in background");
}
}
I Write the below code for executing a task when application is in background state it is working fine in iOS 7 but it is not working in iOS 8.
Can someone give me solution to execute a task continuously in iOS 8 when application is in background state.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
}
There is no way to execute task continuously in background (except in few cases).
Most apps can move to the extended state easily enough but there are
also legitimate reasons for apps to continue running in the
background. The techniques offered by iOS fall into three
categories:
Apps that start a short task in the foreground can ask for time to
finish that task when the app moves to the background.
Apps that initiate downloads in the foreground can hand off management of those downloads to the system, thereby allowing the app to be suspended or
terminated while the download continues.
Apps that need to run in the background to support specific types of tasks can declare their support for one or more background execution modes.
For tasks that require more execution time to implement, you must
request specific permissions to run them in the background without
their being suspended. In iOS, only specific app types are allowed to
run in the background:
Apps that play audible content to the user while in the background,
such as a music player app
Apps that record audio content while in the
background
Apps that keep users informed of their location at all
times, such as a navigation app
Apps that support Voice over Internet
Protocol (VoIP)
Apps that need to download and process new content
regularly
Apps that receive regular updates from external accessories
Apps that implement these services must declare the services they
support and use system frameworks to implement the relevant aspects of
those services. Declaring the services lets the system know which
services you use, but in some cases it is the system frameworks that
actually prevent your application from being suspended.
yes i have implement the background condition while implementing ibeacon. delegate was set in interface
#interface HomeMainVC ()<ESTBeaconManagerDelegate>{
and i was able to send local notification if user enters or exist the beacon region throug following function. So there must be some way to send user location to.
//Beacon manager did enter region
- (void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
{}
//Beacon Manager did exit the region
- (void)beaconManager:(ESTBeaconManager *)manager didExitRegion:(ESTBeaconRegion *)region
{}
You have used following code but it is called only once when application is send to background mode so there is no use of it for you. If you want to do some things only once a time while going in background mode then it is useful for you.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
}
Title says it all.
I've looked at this question and also here on the pubnub forums (same question, just different suggestion).
The core of the issue is that as soon as the application suspends, pubnub connectivity is queued and not sent until the app goes back to foreground. It seems to be a reasonable thing to do to send a notification saying that you're going in the background on your channel but it doesn't work.
From my readings I understand that pubnub uses websockets and that it is not allowed in background mode. Even tried to enable VOIP as a background mode with no luck but Location updates bg mode works. However, using this will have my app rejected as I don't use location services.
When running this code
- (void)applicationDidEnterBackground:(UIApplication *)application {
[PubNub sendMessage:#"Hello from PubNub iOS!" toChannel:self.myChannel;
}
I get this log entry from pubnub (so at least I know the command is ran):
Looks like the client suspended"; Fix suggestion="Make sure that your application is configured to run persistently in background
I have been killing myself over this for a day. One of these days where you start doing something that you think is pretty simple, a 15min thing and it turns into a day of frustration ... You know what I mean :)
I was actually able to send the messages I needed when the app was about to enter Background. And without enabling any of the background modes.
I took advantage of the background finite task as explained is this tutorial.
- (void)applicationWillResignActive:(UIApplication *)application {
[PubNub updateClientState:#"MyID" state:#{#"appState":#"Background",#"userNickname":#"MyNickname"} forObject:[PNChannel channelWithName:#"MyChannel"]];
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(#"Background handler called. Not running background tasks anymore.");
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
}];
}
And implementing the stop background when coming back online
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (self.backgroundTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
NSLog(#"Task invalidated");
}
}
I have searched for this topic bu mostly i face the restrictions of Apple. I need to once per minute control my server and ifthere is a change, fire a local notification. What i need is, how to keep timer(NSTimer) alive in background(or when the device lock is activated..) Any idea please. Thanks
You could do your logic in the server part and if there are changes send a Push Notification.
You need to reconsider the design of your app. You can't guarantee that your app will never be killed when the OS goes out hunting for memory to free up. What happens in that scenario? Push Notifications are your best bet here. First of all, you don't need to be polling your server every 60 seconds; you just fire a notification when the content you're interested in changes on the server. Secondly, the notification will be received even if your app isn't running.
The other issue is that you have to tell Apple, via your info.plist, which background modes your app supports. This is really for apps that run music or VoIP in the background. Polling a web server is not one of those supported modes. With push notifications, you also get some delegate methods you can use to handle the information passed in through the notification when the app enters the foreground.
My app continuously runs in the background with following piece of code.....
-(void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = 0;
backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(backgroundTask) userInfo:nil repeats:YES];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
}
Now do whatever you want in the backgroundTask method.