如何在UITableView中列出已安装的应用程序?

时间:2011-07-14 15:23:19

标签: iphone objective-c xcode uitableview jailbreak

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

3 个答案:

答案 0 :(得分: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;
        }

以下是此示例,假设您的应用名为“ownmadeapp”,并且是应用商店中的应用。

代码:

        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

答案 1 :(得分:0)

有可能,只需看看My3G即可。但我不知道该怎么做。

答案 2 :(得分:0)

NSString * rootAppPath = @“/ Applications”;

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

的NSLog(@ “%@”,listApp);

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