简体   繁体   中英

iOS Private API: wake app from background

I need to have a demo app that will wake up itself from background on timer event. Is it possible without jailbreak by using private API? Tried this code:

void* sbServices = dlopen(SBSERVPATH, RTLD_LAZY);
int (*SBSLaunchApplicationWithIdentifier)(CFStringRef identifier, Boolean suspended) = dlsym(sbServices, "SBSLaunchApplicationWithIdentifier");
int result;
result = SBSLaunchApplicationWithIdentifier(CFSTR("com.my.app"), false);
dlclose(sbServices);

Didn't worked

Finally I found a solution using private api. Here is an example code launching custom app every 10 seconds

@interface PrivateApi_LSApplicationWorkspace

- (bool)openApplicationWithBundleID:(id)arg1;

@end

@implementation ViewController {
    PrivateApi_LSApplicationWorkspace* _workspace;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _workspace = [NSClassFromString(@"LSApplicationWorkspace") new];

    NSTimer *timer = [NSTimer timerWithTimeInterval:10.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [self openAppWithBundleIdentifier:@"com.app.my"];
    }];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

}

- (BOOL)openAppWithBundleIdentifier:(NSString *)bundleIdentifier {
    return (BOOL)[_workspace openApplicationWithBundleID:bundleIdentifier];
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM