iOS 私有 API: 从后台唤醒应用

iOS Private API: wake app from background

我需要一个演示应用程序,它会在定时器事件发生时从后台自行唤醒。使用private API 可以不越狱吗?试过这段代码:

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);

没成功

最后我找到了一个使用私有 api 的解决方案。这是每 10 秒启动自定义应用程序的示例代码

@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