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

2

5

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.

Ordovician answered 24/12, 2011 at 21:18 Comment(0)
L
2

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.

Leialeibman answered 24/12, 2011 at 21:25 Comment(2)
I need it to call a web service every now and then and handle a system callback (awaken to handle an incoming phone call)Ordovician
Okay, then a MobileSubstrate dylib is what you are looking for. You will need to hook the Telephony frameworks, among the other directions you need for the web service. There are tons of MobileSubstrate tutorials on the net. If you do end up needing help, my S.O. profile has ways to contact me.Leialeibman
R
8

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/).

Roosevelt answered 3/4, 2013 at 21:3 Comment(4)
Hi Nate, that's really excellent, but should I use continuous and unboundedTaskCompletion together or just use one of them?which one should I use if I want the app never exit?Thank You!Remedial
@Bob, those are undocumented, so I can't say for sure. However, there's no harm in adding both background modes for a jailbreak app, or app you don't submit to Apple's app store. I have tested this with both modes added to the Info.plist file, and was able to get my app to stay alive as long as I wanted (never exit).Roosevelt
Hello Nate good answer. I've created a LaunchDaemon, but in deep sleep the daemon is also suspended. So will these undocumented background modes work for me?Lubow
@Ashishail, have you looked at the discussion in this question and answer(s)?Roosevelt
L
2

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.

Leialeibman answered 24/12, 2011 at 21:25 Comment(2)
I need it to call a web service every now and then and handle a system callback (awaken to handle an incoming phone call)Ordovician
Okay, then a MobileSubstrate dylib is what you are looking for. You will need to hook the Telephony frameworks, among the other directions you need for the web service. There are tons of MobileSubstrate tutorials on the net. If you do end up needing help, my S.O. profile has ways to contact me.Leialeibman

© 2022 - 2024 — McMap. All rights reserved.