2012-10-16 32 views
1

我正在开发一个下载或查看PDF文件的iPhone应用程序。如何显示可在iOS中打开PDF的外部应用程序列表?

在此应用程序中,我想显示可以查看PDF(iBooks,Adobe Reader等)的可能外部应用程序的列表。我怎么能确定这些应用程序,并点击我的应用程序中的按钮时提出这样一个列表?

+0

可能的答案是[here](http://stackoverflow.com/questions/2774343/how-do-i-associate-file-types-with-an-iphone-application)官方文档是[here](http ://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/PreviewingandOpeningItems.html#//apple_ref/doc/uid/TP40010410-SW1) –

回答

1

您可以通过两种不同的情况来实现这一点,因为这取决于您的要求。在这里我已经包括两种情况,&这将有魅力的工作:

案例1:下载的PDF,把它写在本地&设置的路径。

NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"PDFurlString"]]; 

//Store the Data locally as PDF File 

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"]; 

[pdfData writeToFile:filePath atomically:YES]; 


//Now create Request for the file that was saved in your documents folder 

NSURL *url = [NSURL fileURLWithPath:filePath]; 

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

[webView setUserInteractionEnabled:YES]; 

[webView setDelegate:self]; 

[webView loadRequest:requestObj]; 

案例2:直接浏览PDF使用NSData的WebView中。

[webview loadData:fileData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil]; 

现在,到了最后,如果你正在寻找打开PDF到一些其他的应用程序,

第1步: 使用第一种情况,只要抓住了网址。

第2步: 您可以找到这些url schemes here这些最大的数据库。现在,如何使用?我们需要的只是UIApplication。首先,我们需要检查iOS是否可以打开特定的网址。

[[UIApplication sharedApplication] canOpenURL:[NSURL urlWithString:@"fb://profile"]]; 

步骤3:如果此方法返回是,那么用户安装Facebook应用程序。要打开以下应用程序,您需要拨打:

[[UIApplication sharedApplication] openURL:[NSURL urlWithString:@"fb://profile"]]; 

这将在Facebook应用程序上打开您的Facebook配置文件。

唉,不可能在iOS上禁用任何其他应用程序,因为每个第三方软件都在沙箱中。

希望这对你有帮助。

+0

他想在外部应用程序中打开它! – Eiko

+0

@Eiko我已经根据需求编辑了答案。我确信这将确实帮助用户获得正确的URL并用共享实例打开。 –

+0

他想在外部应用程序中打开。你解释了如何在webview中打开...然后你向我们展示了如何打开Facebook应用程序,但是这与pdfs有关。 –

相关问题