Reputation: 3545
my application is hosting a filesharing web server. And I want that the application is running, even when it in the background. I already know that I can run my application 3 minutes in the background by setting a beginBackgroundTaskWithExpirationHandler
but this is not long enough. Is there a way to keep the app running forever, or at least for a much longer time?
Thanks!
Edit
I finally figured out a working way, its a bit odd but it works.
I have on procedure
-(void)bgTask {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"ApplicationURLScheme"]];
}
that is been called in applicationDidEnterBackground
with
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[application beginBackgroundTaskWithExpirationHandler:^{
[self bgTask];
}];
Thats it, no more code needed, I am running my Webserver in the Background for over an hour now! And the app is not lagging or crashing my iDevice!
Upvotes: 3
Views: 3711
Reputation: 526
Here's what i did for my app when i need the app to run in background to scan for iBeacons forever :
- (void) backgroundForever {
backgroundTaskID = [application beginBackgroundTaskWithExpirationHandler:^{
[self backgroundForever];
}];
}
- (void) stopBackgroundTask {
UIApplication *application = [UIApplication sharedApplication];
if(backgroundTaskID != -1)
[application endBackgroundTask:backgroundTaskID];
}
So basically we start a new background task when the current background task ends. You call stopBackgroundTask to whenever you want to stop the background task. It's up to your application logic, Eg you can call this in applicationDidEnterForeground.
It works, but i'm not sure if your app will pass Apple review.
Upvotes: 2