iOS11 獲取手機已安裝應用列表

在iOS 11 之前咱們可使用LSApplicationWorkspace來獲取手機上已安裝的應用列表git

iOS 11 上獲取全部已安裝應用接口被禁,但能夠根據BundleId檢查App是否存在github

- (BOOL)isInstalled:(NSString *)bundleId {
    NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
    if ([container load]) {
        Class appContainer = NSClassFromString(@"MCMAppContainer");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
        id container = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:bundleId withObject:nil];
#pragma clang diagnostic pop
        NSLog(@"%@", [container performSelector:@selector(identifier)]);
        if (container) {
            return YES;
        } else {
            return NO;
        }
    }
    return NO;
}

此方法在iOS8中不起做用,經筆者驗證,此方法在iOS9以上系統可正確運行。app

 

老方法LSApplicationWorkspace,經筆者驗證在iOS8 ~ iOS10都可正常獲取全部已安應用ide

附老方法代碼工具

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
    SEL selector    =   NSSelectorFromString(@"defaultWorkspace");
    NSObject* workspace = [LSApplicationWorkspace_class performSelector:selector];
    SEL selectorALL = NSSelectorFromString(@"allInstalledApplications");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
    NSArray *allInstallApps = [workspace performSelector:selectorALL];
    
    for (NSObject *obj in allInstallApps) {
        NSString *appBundleId   = [obj performSelector:@selector(applicationIdentifier)];
        NSLog(@"installed %@", appBundleId);
    }
#pragma clang diagnostic pop    // -Wundeclared-selector
#pragma clang diagnostic pop    // -Warc-performSelector-leaks

 

更多私有API探索,感興趣的同窗可用此工具瀏覽,不幸的是貌似在iOS11上不能完美運行spa

相關文章
相關標籤/搜索