iphone - 是否可以获取 iPhone 上安装的所有应用程序的信息?

标签 iphone

是否可以获取 iPhone/iPod 上已安装的所有应用程序的信息(应用程序图标、应用程序名称、应用程序位置)?

最佳答案

有一种方法可以检查应用程序是否已安装,但是,它确实违反了沙盒规则,Apple *可能会拒绝您的应用程序使用此功能。但 App Store 中提供的其他应用程序之前已经做过此操作,因此请随意尝试

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

// 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

在使用之前先尝试一下,我认为 Apple 更改了 MobileInstallation.plist 的位置,如果您确实更改了它,请在实际设备而不是模拟器上尝试。祝你好运!

http://www.iphonedevsdk.com/forum/iphone-sdk-development/37103-finding-out-what-apps-installed.html

PK

关于iphone - 是否可以获取 iPhone 上安装的所有应用程序的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3878197/

相关文章:

iphone - UITextField - 圆角问题

iphone - 安装 IPA 导致错误 "A signed resource has been added, modified, or deleted"

iOS – 通过 Wi-Fi 运行/调试/安装构建

iphone - 如何通过 UISlider 更新 iPhone 上的音乐音量?

iphone - 如何在横向模式下创建像 iPod 应用程序一样的封面流菜单?

iphone - 将 'stringWithString:' 与文字一起使用是多余的

iphone - iOS: locationManager:didUpdateToLocation:fromLocation: 不移动时报告速度

iphone - AFNetworking HTTP POST上传图像即将完成时停止

ios - 如何检测用户是否在我的应用程序中为本地通知设置了横幅样式警报或 'normal' 样式警报?

iphone - iPhone试图让YouTube自动启动视频