如何检查特定应用程序是否已安装在iphone设备上?
答
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
自定义URL方案:使用下面的代码
http://mobiledevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
答
可以检索所有应用程序的列表。
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];
答
如果是否存在这样的应用程序有一个注册的URL,您可以检查
[[UIApplication sharedApplication] canOpenURL:url]
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
(如果我记得很清楚)。
答
下面是测试facebook应用程序是否已安装的示例。
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {
// Facebook app is installed
}
你会详细说明吗?如果安装了另一个应用程序,您想从一个应用程序中检查吗? – netigger 2012-07-31 09:15:07