iOS 6 - Able to save .pdf from URL, able to display it in a WebView, NOT able to move it to iBooks

StackOverflow https://stackoverflow.com/questions/14772439

Question

This is what I'm trying to do:

  1. Get a .pdf from external URL
  2. Save it into my local disk
  3. Display it in a WebView
  4. Allow the user to move the .pdf to another app who can read .pdf

Everything from 1 to 3 works fine. But nothing is moved/shared to/with other apps. I can't understand what I'm doing wrong. This is what I'm doing.

How I save the pdf in the Documents folder (viewDidLoad):

// to save the pdf into local file system (tempString is the pdf url)
NSData *pdfData = [[NSData alloc] 
    initWithContentsOfURL:[NSURL URLWithString:tempString]];
NSString *resourceToPath = [[NSString alloc] 
    initWithString:[[[[NSBundle mainBundle] resourcePath]
    stringByDeletingLastPathComponent] 
    stringByAppendingPathComponent:@"Documents"]];
NSString *filePAth = [resourceToPath stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:filePAth atomically:YES];

// to populate the WebView
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[my_web_view setUserInteractionEnabled:YES];
//[editoriale_view setDelegate:self];
[my_web_view loadRequest:requestObj];

In my viewDidLoad() function I create a button to allow the user to open a list of apps who can read .pdf files:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self 
    action:@selector(show_Button)];

And here's my show_Button function:

-(void)show_Button {
    NSString *resourceToPath = [[NSString alloc] 
        initWithString:[[[[NSBundle mainBundle] resourcePath] 
        stringByDeletingLastPathComponent] 
        stringByAppendingPathComponent:@"Documents"]];
        NSString *filePAth = [resourceToPath 
        stringByAppendingPathComponent:@"myPDF.pdf"];

    NSLog(@"filePath = %@", filePAth);
    NSURL *url2 = [NSURL fileURLWithPath:filePAth];    
    NSLog(@"url2 = %@", url2);

    UIDocumentInteractionController *docContr = [UIDocumentInteractionController 
        interactionControllerWithURL:url2];
    [docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
 }

When I try this on my device everything works fine until I tap on one of the icons in the list (i.e. the iBooks one). Then the app closes (it doesn't crash, it simply closes).

Here's what the console prints for the two logs I put in the show_Button function:

1. filePath = /Users/[MY_USER]/Library/Application Support/iPhone
    Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf
2. url2 = file://localhost/Users/[MY_USER]/Library/Application%20Support/
    iPhone%20Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf

Anyone wants to try to make me understand what I'm doing wrong? I'm using Xcode 4.6. I browsed my iPhone app file system with a third-party software and the file "MyPDF.pdf" actually IS in the Documents" folder, and that's clear because the WebView is correctly populated.

Was it helpful?

Solution 2

Solved. I had not implemented the UIDocumentenInteractionController delegate in the .h file. Now I have and everything works fine. Thank you to @trojanfoe for the useful hint.

OTHER TIPS

Change CGRectZero to self.view.bounds when you display the document controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top