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.

britta
  • 155
  • 8
Zaky German
  • 14,324
  • 4
  • 25
  • 31

2 Answers2

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

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • Most welcome. I was overjoyed to find out about this undocumented feature recently :) – Nate Apr 04 '13 at 10:31
  • 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! – Suge Jul 04 '13 at 07:11
  • 1
    @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). – Nate Jul 04 '13 at 08:11
  • 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? – JOA80 Aug 22 '13 at 10:24
  • @Ashishail, have you looked at the discussion [in this question and answer(s)?](http://stackoverflow.com/q/15723432/119114) – Nate Aug 23 '13 at 19:35
  • @Nate, yes I've gone through that, most of the things I've tried . I know power management will work, but that will keep the phone running, so trying to avoid that. I can also do a `idleTimerDisable`on Spring Board but it will be same. Background modes don't work on daemon because `[UIApplication sharedApplication]` is null. what about dylib/tweak. But u suggested to post notification from dylib, but if my daemon is sleeping I'll never get the notification. can i wake the process? via signal or something? one thing I noticed is, it wakes up if I get a change on socket, like a disconnection. – JOA80 Aug 24 '13 at 14:36
  • @Ashishail, the `UIBackgroundModes`, as the name suggests, are only for UIApplications. Concerning the problem with your Launch Daemon, I was suggesting you look at drewmm's solution in the question I linked to (his accepted answer). If you don't want to do that, then I'm not sure what to suggest. You kind of need to let the phone sleep, or not sleep ... it can't be both. On my phone, I have no problems with this, and I don't see noticeable battery drain. The important thing is not to keep the screen lit, the GPS on, or network active. Those are big drains. – Nate Aug 24 '13 at 23:21
  • @Nate Thanks for the tip. I've got a client that needs to have an application running in the background indefinitely. I can distribute via Enterprise iOS Distribution, so there's no concern of App Store approval process. You indicated that the two UIBackgroundModes mentioned will only work if the application is installed in /Applications and not in the normal sandbox area /var/mobile/Applications. Is there a way to install to /Applications without jailbreaking the iphone? – blueether Apr 04 '15 at 17:54
  • 1
    @blueether, none that I know of, no. Also, this technique in general stopped working a while ago (maybe iOS 7?). It's possible that this can still be *part* of a working solution, but that something more is needed now. I tried finding better alternatives a few months ago, but all of them had drawbacks. – Nate Apr 06 '15 at 19:52
  • Hi @Nate I know this is an ages long question, but don't suppose you ever had any luck with later versions of iOS (without jailbreak)? – simonthumper Feb 22 '19 at 16:12
  • @simonthumper, I have, although that app isn't current (iOS 12). It also depends on whether this is for the app store or not. If not for the store, you can exploit various background modes (audio, VOIP, location) to keep an app alive. There will be serious battery drain implications, depending on how long you can let the app sleep (if at all). – Nate Feb 22 '19 at 23:31
  • @Nate to give some context, it's to keep an app alive which will monitor the charging state of the test devices in our office and will require the user to login when they unplug a device. I've tried with audio and location background modes, but whatever I'm doing doesn't seem to keep running indefinitely! I was under the impression that the VOIP hack no longer works as it now relies on a special type of push notification? Battery drain isn't too much of an issue as the devices spend most of their time charging! :D – simonthumper Feb 24 '19 at 10:33
  • @simonthumper, you really should post a new question for this. By needing a solution w/o jailbreak, your situation is not the same as the OP here, and thus, this section is not a good place to deal w/ it. I'd certainly encourage you to link a new question to this one ("what have you tried"), and tag it with `iphone-privateapi` if you're willing to accept solutions that wouldn't pass App Store review. I can't guarantee I can look at it, but you're welcome to ping me when the new question is live. My feeling is that it's solvable. – Nate Feb 25 '19 at 07:26
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.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • I need it to call a web service every now and then and handle a system callback (awaken to handle an incoming phone call) – Zaky German Dec 24 '11 at 23:46
  • 1
    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. – WrightsCS Dec 24 '11 at 23:52