【问题标题】:How can i list the installed apps in a UITableView?如何在 UITableView 中列出已安装的应用程序?
【发布时间】:2011-07-14 15:23:19
【问题描述】:

我正在尝试获取 iPhone 上所有已安装应用程序的列表(越狱),然后在 UITableView 中使用它们的图标列出它们......这可能吗?谢谢

【问题讨论】:

    标签: iphone objective-c xcode uitableview jailbreak


    【解决方案1】:

    它们是一种检查应用程序是否已安装的方法。有时您可能想检查设备上是否安装了特定应用程序,以防您使用需要安装其他应用程序的自定义 URL 方案(然后您可以灰显/禁用某些按钮)。不幸的是,Apple 显然没有任何功能可以为您检查这个,所以我做了一个。它不会枚举每个应用程序,而是使用 MobileInstallation 缓存,该缓存始终与 SpringBoard 保持同步,并保存所有已安装应用程序的信息字典。尽管您“不应该”访问缓存,但它可以读取我的 App Store 应用程序。这是我的代码,至少可以在模拟器上完美运行:

            // Declaration
            BOOL APCheckIfAppInstalled(NSString *bundleIdentifier); // Bundle identifier (eg. com.apple.mobilesafari) used to track apps
    
                // Implementation
    
            BOOL APCheckIfAppInstalled(NSString *bundleIdentifier)
            {
                static NSString *const cacheFileName = @"com.apple.mobile.installation.plist";
                NSString *relativeCachePath = [[@"Library" stringByAppendingPathComponent: @"Caches"] stringByAppendingPathComponent: cacheFileName];
                NSDictionary *cacheDict = nil;
                NSString *path = nil;
                    // Loop through all possible paths the cache could be in
                for (short i = 0; 1; i++)
                {
    
                    switch (i) {
                        case 0: // Jailbroken apps will find the cache here; their home directory is /var/mobile
                            path = [NSHomeDirectory() stringByAppendingPathComponent: relativeCachePath];
                            break;
                        case 1: // App Store apps and Simulator will find the cache here; home (/var/mobile/) is 2 directories above sandbox folder
                            path = [[NSHomeDirectory() stringByAppendingPathComponent: @"../.."] stringByAppendingPathComponent: relativeCachePath];
                            break;
                        case 2: // If the app is anywhere else, default to hardcoded /var/mobile/
                            path = [@"/var/mobile" stringByAppendingPathComponent: relativeCachePath];
                            break;
                        default: // Cache not found (loop not broken)
                            return NO;
                        break; }
    
                    BOOL isDir = NO;
                    if ([[NSFileManager defaultManager] fileExistsAtPath: path isDirectory: &isDir] && !isDir) // Ensure that file exists
                        cacheDict = [NSDictionary dictionaryWithContentsOfFile: path];
    
                    if (cacheDict) // If cache is loaded, then break the loop. If the loop is not "broken," it will return NO later (default: case)
                        break;
                }
    
                NSDictionary *system = [cacheDict objectForKey: @"System"]; // First check all system (jailbroken) apps
                if ([system objectForKey: bundleIdentifier]) return YES;
                NSDictionary *user = [cacheDict objectForKey: @"User"]; // Then all the user (App Store /var/mobile/Applications) apps
                if ([user objectForKey: bundleIdentifier]) return YES;
    
                    // If nothing returned YES already, we'll return NO now
                return NO;
            }
    

    这是一个示例,假设您的应用名为“yourselfmadeapp”并且是应用商店中的一个应用。

    代码:

            NSArray *bundles2Check = [NSArray arrayWithObjects: @"com.apple.mobilesafari", @"com.yourcompany.yourselfmadeapp", @"com.blahblah.nonexistent", nil];
    
            for (NSString *identifier in bundles2Check)
    
            if (APCheckIfAppInstalled(identifier))
    
            NSLog(@"App installed: %@", identifier);
    
            else
    
            NSLog(@"App not installed: %@", identifier);
    

    日志输出:

    代码:

            2009-01-30 12:19:20.250 SomeApp[266:20b] App installed: com.apple.mobilesafari
    
            2009-01-30 12:19:20.254 SomeApp[266:20b] App installed: com.yourcompany.yourselfmadeapp
    
            2009-01-30 12:19:20.260 SomeApp[266:20b] App not installed: com.blahblah.nonexistent
    

    【讨论】:

    • 为什么他们总是删除该死的.proj文件
    • @Cykey:您应该将您的评论移至单独的答案并接受它。
    • @SeungUnHam:没有 .proj 文件,因为它不打算在 Xcode 中编译,很可能使用 Theos。
    【解决方案2】:

    有可能,看看 My3G。不过,我不知道该怎么做。

    【讨论】:

      【解决方案3】:

      NSString *rootAppPath = @"/Applications";

      NSArray *listApp = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:rootAppPath error:nil];

      NSLog(@"%@",listApp);

      这是获取所有已安装的应用程序,然后使用此数组在 tableview 中显示的方法之一

      【讨论】:

        猜你喜欢
        • 2018-07-21
        • 1970-01-01
        • 2020-02-01
        • 2011-01-16
        • 2023-01-07
        • 1970-01-01
        • 2013-03-18
        • 2010-10-22
        • 2014-03-23
        相关资源
        最近更新 更多