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

折月煮酒 提交于 2019-11-29 04:55:30

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.

Nate

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!