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.
Related
I am building an app which uploads files to the server via ajax.
The problem is that users most likely sometimes won't have the internet connection and client would like to have the ajax call scheduled to time when user have the connection back. This is possible that user will schedule the file upload when he's offline and close the app. Is it possible to do ajax call when the application is closed (not in background)?
When app is in the background, we can use background-fetch but I have never faced a problem to do something when app is closed.
The short answer is no, your app can't run code after being terminated.
You can run code in your AppDelegate's applicationWillTerminate, but that method is mainly intended for saving user data and other similar tasks.
See this answer also.
Yes you can do stuff in the background. You are limited to several different background modes of execution. Server communication is one of the modes that is allowed (background fetch). Make sure you set the properties correctly in Xcode per the guidelines (i.e. don't say you are a audio app when you are doing data transfer). See the details here:
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
I found your question because it has the Cordova tag associated with it. If you are using Cordova you can use this plugin here to manage things in the background:
https://github.com/katzer/cordova-plugin-background-mode
Edit: If the user is FORCE closing / terminating the app then there is nothing you can do. If they are just exiting the app to the home screen and use other apps, then you can run in the background.
The other option you can do is schedule a local notification to upload the file. If you app successfully uploads the file because it is open / has a connection / did it in the background, then you cancel the local notification.
If the local notification isn't cancelled it will remind the user the file is not uploaded and when they open the notification your app will start back where it left off.
- (void)applicationWillTerminate:(UIApplication *)application {
if (application) {
__block UIBackgroundTaskIdentifier backgroundTask = UIBackgroundTaskInvalid;
backgroundTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
backgroundTask = UIBackgroundTaskInvalid;
}];
[self doSomething];
[application endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}
}
I have already answered a similar question:
https://stackoverflow.com/a/57245917/6157415
You can implement notification, when user receive notification part of your code can be executed.
in particular there are Silent Push Notification to do this:
Sometimes, you may want to use a Silent Push Notification to update
content inside you app in the background. A silent push notification
is defined as a push that does not have an alert, badge or sound, and
just has Key-Value data.
From: https://docs.mobile.sailthru.com/docs/silent-push-notifications
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.
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");
}
}
Recently I have to implement background working app with voip. For that I use PJSip.
Right now I have done registering and handling calls working perfectly when app is running.
When app goes to the background, by first 10 minuts works fine -> new incomming calls are captured and are send as local notifications - so this is fine. After 10 minutes sip stops working and incomming calls doesn't arrives as well.
Could you help me with this?
I have check "bacground working - VoiP"
I have done keep alive in pjsip from siphon2 example application.
I have also:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[PjSipService service] setIsInBackground:YES];
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
int KEEP_ALIVE_INTERVAL = 600;
[self performSelectorOnMainThread:#selector(keepAlive) withObject:nil waitUntilDone:YES];
NSLog(#"startingKeepAliveTimeout1");
[application setKeepAliveTimeout:KEEP_ALIVE_INTERVAL handler: ^{
NSLog(#"startingKeepAliveTimeout2");
[self performSelectorOnMainThread:#selector(keepAlive) withObject:nil waitUntilDone:YES];
}];
__block UIBackgroundTaskIdentifier bgTask;
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//[self deregis];
[application endBackgroundTask: bgTask]; //End the task so the system knows that you are done with what you need to perform
bgTask = UIBackgroundTaskInvalid; //Invalidate the background_task
NSLog(#"\n\nRunning in the background!\n\n");
});
}
"keepAlive" function is empty.
I've read that for this function keepAlive is enought - but without background task it doesn't work even 10 minuts.
Application has to works in iOS7 and iOS6
Apps running in background, is performed using setKeepAliveTimeout method in IOS. But from later versions, setKeepAliveTimeout method is deprecated.
You want to use Remote notifications to receive incoming call. particularly for VOIP incoming calls, apple introduced Callkit framework. please follow that framework to accept incoming calls while your VOIP App is in background state.
https://www.raywenderlich.com/150015/callkit-tutorial-ios
I am working on the same project with same requirements and I came across with the same issue recently. I tried many different solutions and finally I found something that is working for now.
Please note that I recently found this solution and I am working on this. I haven't consider the reaction of this code at least for now.
Sample Code :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self sipConnect];
backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundTask];
}];
}
-(void) endBackgroundTask
{
[[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
backgroundTask = UIBackgroundTaskInvalid;
}
You probably use UPD. To make sure you use TCP, when setting up your registrar you need to specify something like this sip:ip_address:port;transport=tcp
Apple has deprecated keepAliveTimeoutHandler. To receive calls when applicationDidEnterBackground you have to use PushKit framework. Read documentation for more information here.
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.