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

前端 未结 2 959
遥遥无期
遥遥无期 2020-12-17 03:53

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 backgr

2条回答
  •  情书的邮戳
    2020-12-17 04:56

    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:

    UIBackgroundModes
    
        continuous
        unboundedTaskCompletion
    
    

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

提交回复
热议问题