2012-07-31 161 views

回答

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 

自定义URL方案:使用下面的代码

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

这仅适用于应用程序已设置注册且已知的URL方案。 – Martin 2012-07-31 09:25:05

+0

你打算怎么注册? http://mobiledevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html – Alex 2012-07-31 09:29:52

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]; 
0

如果是否存在这样的应用程序有一个注册的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(如果我记得很清楚)。

0

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

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