Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping an app alive in background unlimited (for a Cydia app)

I don't mind using private API's or anything of the kind that Apple doesn't like, but would prefer a quick solution that doesn't stuff like playing silence in the background or swizzling.

Obviously this isn't for the app store so please no lecturing :)

So how do you run in the background without any restrictions like "backgrounder"? I didn't manage to find an answer besides some that point people to different directions, but maybe since then someone managed to dig it up already.

like image 688
Zaky German Avatar asked Dec 24 '11 21:12

Zaky German


People also ask

Can iOS app run in background?

No. Applications cannot run in the background for over 10 minutes, except for a few certain situations (VOIP, playing audio, etc.) An iOS app which plays audio will keep playing audio indefinitely in the background so long as the audio is playing.

How long iOS app can run in background?

Tasks are under a strict time limit, and typically get about 600 seconds (10 minutes) of processing time after an application has moved to the background on iOS 6, and less than 10 minutes on iOS 7+.


2 Answers

Update:

This solution no longer appears to be sufficient (~ iOS 7+ or 7.1+). I'm leaving the original answer for historical reference, and in case it helps produce a future solution based on this obsolete one:


It depends on what you mean by app. If you're talking about a non-graphical background service, then what you want is a Launch Daemon. See here for how to create a launch daemon.

If you have a normal UI application, but when the user presses the home button, you want it to stay awake in the background for an unlimited time, then you can use some undocumented Background Modes in your app's Info.plist file:

<key>UIBackgroundModes</key>
<array>
    <string>continuous</string>
    <string>unboundedTaskCompletion</string>
</array>

Then, when iOS is ready to put your app into the background (e.g. user presses home button), you can do this, in your app delegate:

@property (nonatomic, assign) UIBackgroundTaskIdentifier bgTask;


- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Delay execution of my block for 15 minutes.
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 15 * 60 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        NSLog(@"I'm still alive!");
    });

    self.bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        // should never get here under normal circumstances
        [application endBackgroundTask: self.bgTask]; 
        self.bgTask = UIBackgroundTaskInvalid;
        NSLog(@"I'm going away now ....");
    }];
}

Normally, iOS only gives you up to 10 minutes for your UI application to work in the background. With the undocumented background mode, you'll be able to keep alive past that 10 minute limit.

Note: this does not require hooking with MobileSubstrate. If you're using the second method (undocumented Background Modes), then it does require installing your app in /Applications/, not in the normal sandbox area (/var/mobile/Applications/).

like image 157
Nate Avatar answered Oct 08 '22 11:10

Nate


Depending on what your "app" is going to do, you can hook MobileSubstrate. This will load with SpringBoard and essentially run "in the background".

If you want to write an actual application, then you can also write a "Dynamic Library" which will be loaded with SpringBoard by MobileSUbstrate. You can talk back and forth between this dylib and your app by using NSNotificationCenter; creating and posting notifications.

like image 25
WrightsCS Avatar answered Oct 08 '22 11:10

WrightsCS