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


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

iOS 11 上獲取所有已安裝應用接口被禁,但可以根據BundleId檢查App是否存在

- (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以上系統可正確運行。

 

老方法LSApplicationWorkspace,經筆者驗證在iOS8 ~ iOS10均可正常獲取所有已安應用

附老方法代碼

#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上不能完美運行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM