I have enabled Background Fetch from capabilities.
Please give me solution that once user open the application, it should not closed automatically.
Adding UIBackgroundModes does not make you app keep running in background.
Each of the preceding modes lets the system know that your app should be woken up or launched at appropriate times to respond to relevant events. For example, an app that begins playing music and then moves to the background still needs execution time to fill the audio output buffers. Enabling the Audio mode tells the system frameworks that they should continue to make the necessary callbacks to the app at appropriate intervals. If the app does not select this mode, any audio being played or recorded by the app stops when the app moves to the background.
You will have to either have a task running or callbackHandlers to respond when OS send any signal from the background task as shown in example from Apple
- (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;
});
}
Please follow Background Execution for more details.
Related
I have a situation where I must call a web service before app termination but not when enters the background, because this should be done only once which when the app get terminated.
I tried the background execution thing but I believe it won't work once the app get terminated but only if the app in the background.
Here is my latest try inside applicationWillTerminate:
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Call web service here
[application endBackgroundTask:bgTask];
bgTask =UIBackgroundTaskInvalid;
});
You can't do that, and honestly, why would you?
This is an ill-posed problem anyway. What should happen if the battery dies? What if the app crashes? What if the app goes to background and then the OS terminates it when memory pressure increases? What if you lose internet access and the app is terminated?
The closest thing you can do is to implement some sort of heartbeat service which your app regularly pings. If the pinging stops, your app has either terminated or network connectivity was lost.
If it possible to run a service after app was terminated. Please, help me verify following code:
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
Your app will generally never see willTerminate, because the system generally only terminates your app once it's already suspended (in the background). Once your app is suspended, it gets no further chance to act(*), so there's no callback for that.
The didEnterBackground delegate message or notification should be considered your last chance to clean things up or save state before possible termination.
(*) Okay, your app can do stuff if it's in one of the supported background execution modes, like audio, VoIP, or navigation, but in that case it either hasn't been suspended yet or it's been un-suspended with an entry point specific to that background mode.
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 developed an app that makes a http request (async call) when server responses at that time if iPhone is locked. network connection fails, i am getting an error code -1005 "network connection fail". how can i maintain network connection when phone is locked and allow the app to receive response when phone is unlocked again or in background. Also app crashes when user kills app when receiving response.
You should take a look at Background Execution and Multitasking. It allows your app to function up to about 10 minutes while in background.
Here's sample code from the doc:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application 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), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}