1

I am developing an iPhone application that downloads or views PDFs.

Within this application, I would like to display a list of possible external applications that can view PDFs (iBooks, Adobe Reader, etc.). How could I determine these applications and bring up such a list when clicking on a button in my application?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
franmarquezr
  • 53
  • 1
  • 7
  • Possible answer is [here](http://stackoverflow.com/questions/2774343/how-do-i-associate-file-types-with-an-iphone-application) Official documentation is [here](http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/PreviewingandOpeningItems.html#//apple_ref/doc/uid/TP40010410-SW1) – SpaceDust__ Oct 16 '12 at 13:35

1 Answers1

1

You can achieve this via two different cases as it depends on your requirements. Over here I have included both of the cases, & this will work with charm :

Case 1: Download the PDF, write it locally & set the path.

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

Case 2: View the PDF directly in WebView using NSData.

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

Now, at the end,if you are looking to open the PDF into some other App,

Step 1 : Use the First Case, just grab the url.

Step 2 : You can find the biggest database of those url schemes here. Now, how to use? All that we'll need is UIApplication. First, we need check if the iOS can open specific url.

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

Step 3: If this method returns yes then the user has the facebook application installed. To open the following application you need to call:

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

Which will open your facebook profile on the facebook application.

Alas, there is no possibility of disabling any other application on the iOS, since each and every third party software is being sandboxed.

Hope this was helpful to you.

Ajay Sharma
  • 4,509
  • 3
  • 32
  • 59
  • He wants to open it in an external application! – Eiko Oct 17 '12 at 09:01
  • @Eiko I have edited the answer according to the requirement.I am sure this would really help the user one he get the proper url to open with shared instance. – Ajay Sharma Oct 17 '12 at 09:16
  • he wants to open in external app. You explain how to open inside a webview... and then you show us how to open the facebook app but that has litle to do with pdfs.. – Radu Simionescu Jul 21 '15 at 13:38