iOS 獲取手機安裝的所有的Apps

#import <objc/runtime.h>

- (void)installedApplications
{
    Class lsawsc = objc_getClass("LSApplicationWorkspace");
    NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
    NSArray *apps = [workspace performSelector:NSSelectorFromString(@"allInstalledApplications")];


    Class LSApplicationProxy_class = objc_getClass("LSApplicationProxy");
    for (int i = 0; i < apps.count; i++) {
        NSObject *temp = apps[i];
        if ([temp isKindOfClass:LSApplicationProxy_class]) {
            //應用的bundleId
            NSString *appBundleId = [temp performSelector:NSSelectorFromString(@"applicationIdentifier")];
            //應用的名稱
            NSString *appName = [temp performSelector:NSSelectorFromString(@"localizedName")];
            //應用的類型是系統的應用還是第三方的應用
            NSString * type = [temp performSelector:NSSelectorFromString(@"applicationType")];
            //應用的版本
            NSString * shortVersionString = [temp performSelector:NSSelectorFromString(@"shortVersionString")];
        
            NSString * resourcesDirectoryURL = [temp performSelector:NSSelectorFromString(@"containerURL")];
        
            NSLog(@"類型=%@應用的BundleId=%@ ++++應用的名稱=%@版本號=%@\n%@",type,appBundleId,appName,shortVersionString,resourcesDirectoryURL);
        
        }
    }  
}

//調用App  
- (void)openApp {
    PrivateApi_LSApplicationWorkspace* workspace = [NSClassFromString(@"LSApplicationWorkspace") new];      
    [workspace openApplicationWithBundleID:@"com.xxx.xxx"];  
}

//獲取所有App包名
1.獲取到手機里面所有的APP包名

- (void)touss
{
    Class lsawsc = objc_getClass("LSApplicationWorkspace");
    NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
    NSArray *Arr = [workspace performSelector:NSSelectorFromString(@"allInstalledApplications")];
    for (NSString * tmp in Arr)
    {
        NSString * bundleid = [self getParseBundleIdString:tmp];
        NSLog(@"%@",bundleid);
    }
}

- (NSString *)getParseBundleIdString:(NSString *)description
{
    NSString * ret = @"";
    NSString * target = [description description];
    // iOS8.0 "LSApplicationProxy: com.apple.videos",
    // iOS8.1 "<LSApplicationProxy: 0x898787998> com.apple.videos",
    // iOS9.0 "<LSApplicationProxy: 0x145efbb0> com.apple.PhotosViewService <file:///Applications/PhotosViewService.app>"
    if (target == nil) {
        return ret;
    }
    NSArray * arrObj = [target componentsSeparatedByString:@" "];
    switch ([arrObj count]) {
        case 2: // [iOS7.0 ~ iOS8.1)
        case 3: {
             // [iOS8.1 ~ iOS9.0)
            ret = [arrObj lastObject];
        }
          break;
        case 4: {
            // [iOS9 +) 
            ret = [arrObj objectAtIndex:2];
        }
          break;
        default:
            break;
    }
    return ret;
}

2.通過包名去打開應用

    Class lsawsc = objc_getClass("LSApplicationWorkspace");
    NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
    // iOS6 沒有defaultWorkspace
    if ([workspace respondsToSelector:NSSelectorFromString(@"openApplicationWithBundleID:")])
    {
        [workspace performSelector:NSSelectorFromString(@"openApplicationWithBundleID:") withObject:@"com.Calendar.jbp"];
    }
更新 來源()

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

2020年02月27更新

企業(yè)版安裝和 測試的APP 沒搜到信息

 id space = [NSClassFromString(@"LSApplicationWorkspace") performSelector:@selector(defaultWorkspace)];
    NSArray *plugins = [space performSelector:@selector(installedPlugins)];
    NSMutableSet *list = [[NSMutableSet alloc] init];
    for (id plugin in plugins) {
        id bundle = [plugin performSelector:@selector(containingBundle)];
        if (bundle)
            [list addObject:bundle];
    }
    int a = 1;
    for (id plugin in list) {
        NSLog(@"?? %d--",a);
        a++;
        NSLog(@"bundleIdentifier =%@", [plugin performSelector:@selector(bundleIdentifier)]);//bundleID
        
        NSLog(@"applicationDSID =%@", [plugin performSelector:@selector(applicationDSID)]);
        NSLog(@"applicationIdentifier =%@", [plugin performSelector:@selector(applicationIdentifier)]);
        NSLog(@"applicationType =%@", [plugin performSelector:@selector(applicationType)]);
        NSLog(@"dynamicDiskUsage =%@", [plugin performSelector:@selector(dynamicDiskUsage)]);

        NSLog(@"itemID =%@", [plugin performSelector:@selector(itemID)]);
        NSLog(@"itemName =%@", [plugin performSelector:@selector(itemName)]);
        NSLog(@"minimumSystemVersion =%@", [plugin performSelector:@selector(minimumSystemVersion)]);
        
        NSLog(@"requiredDeviceCapabilities =%@", [plugin performSelector:@selector(requiredDeviceCapabilities)]);
        NSLog(@"sdkVersion =%@", [plugin performSelector:@selector(sdkVersion)]);
        NSLog(@"shortVersionString =%@", [plugin performSelector:@selector(shortVersionString)]);
        
        NSLog(@"sourceAppIdentifier =%@", [plugin performSelector:@selector(sourceAppIdentifier)]);
        NSLog(@"staticDiskUsage =%@", [plugin performSelector:@selector(staticDiskUsage)]);
        NSLog(@"teamID =%@", [plugin performSelector:@selector(teamID)]);
        NSLog(@"vendorName =%@", [plugin performSelector:@selector(vendorName)]);
    }
    return;

顯示一個app的信息

只顯示一個app的信息
 bundleIdentifier =com.mobike.bike
 applicationDSID =8124785344
 applicationIdentifier =com.mobike.bike
 applicationType =User
 dynamicDiskUsage =67100672
 itemID =1044535426
 itemName =摩拜單車-好騎可靠的共享單車
 minimumSystemVersion =12.0
 requiredDeviceCapabilities =(
 sdkVersion =12.1
 shortVersionString =8.12.1
 sourceAppIdentifier =(null)
 staticDiskUsage =88735744
 teamID =2M4WW2KKY5
 vendorName =Beijing Mobike Technology Co., Ltd.

慢慢來,一步一個巴掌印~~~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,157評論 25 708
  • 還記得小時候 從學?;丶业穆飞?踢著石子,想著乘法口訣 家里有準備的辣椒炒蛋 稍長大些 帶著女朋友回家 爸媽備了一...
    李修名閱讀 407評論 2 10
  • 2017年9月9日似乎看起來是個數字很好的一天。 今天是周六,因為打算加一會班就去逛街,所以就簡單的白襯衣,白鞋,...
    西橙L閱讀 270評論 0 0
  • 如果每次上車時不能讓你感到興奮、那它只是個移動工具而已。
    好好先森灬閱讀 228評論 0 0
  • 摘要 真的是說吃的蔥,沒有任何隱喻或指桑罵槐。 引言 我以前是個買菜常常會忘記買蔥的人,每每到了下鍋前,發(fā)現少了這...
    55ebf88820a0閱讀 463評論 1 1

友情鏈接更多精彩內容