如何检查特定的应用程序是否已经安装在iPhone设备上?

时间:2012-07-31 09:13:05

标签: iphone

如何检查特定应用程序是否已安装在iphone设备上? 我们怎么能实现这个想法?

4 个答案:

答案 0 :(得分:1)

canOpenURL实质上是检查是否已安装注册到该特定URL方案的应用程序,或者换句话说,如果该应用程序存在,如果是,则我们可以打开该URL。

- (BOOL) appExists: (NSURL*)url{
    if ([[UIApplication sharedApplication] canOpenURL:url]) {        
        return YES;

    } else {
        return NO;
    }
}


NSURL *urlApp = [NSURL URLWithString:@"fb://profile/73728918115"];// facebook app

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter:///user?screen_name=INNOVA_ET_BELLA"]];//tweeter app

if ([self appExists:urlApp]) {
        [[UIApplication sharedApplication] openURL:urlApp];
} 

IPhone URL方案:

http://wiki.akosma.com/IPhone_URL_Schemes

自定义网址架构:

http://mobiledevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html

答案 1 :(得分:0)

使用以下代码,您可以检索所有应用程序的列表。

 NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
 NSString *sandBoxPath = [bundleRoot stringByDeletingLastPathComponent];
 NSString *appFolderPath = [sandBoxPath stringByDeletingLastPathComponent];

 NSFileManager *fm = [NSFileManager defaultManager];
 NSArray *dirContents = [fm contentsOfDirectoryAtPath:appFolderPath error:nil];

 NSMutableArray *appNames = [[NSMutableArray alloc]init];
 for(NSString *application in dirContents)
 {
      NSString *appPath = [appFolderPath stringByAppendingPathComponent:application];
      NSArray *appcontents = [fm contentsOfDirectoryAtPath:appPath error:nil];
      NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.app'"];
      NSArray *onlyApps = [appcontents filteredArrayUsingPredicate:fltr];
      if(onlyApps.count > 0)
           [appNames addObject:[onlyApps objectAtIndex:0]];
 }

 NSLog(@"%@", [appNames description]);
 [appNames release];

答案 2 :(得分:0)

如果应用程序有注册URL,您可以检查是否存在这种方式

[[UIApplication sharedApplication] canOpenURL:url]

网址类似于skype://

如果没有,则需要循环所有文件夹 在这里如何:Getting a list of files in a directory with a glob

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.app'"];
NSArray *onlyAPPs = [dirContents filteredArrayUsingPredicate:fltr];

应用程序位于/var/application/APPNAME.app(如果我记得很清楚)。

答案 3 :(得分:0)

以下是测试Facebook应用程序是否已安装的示例。

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {
// Facebook app is installed
 }