3

I am making an app that downloads a file from an online server and save it to the App's local memory. Now, what I want to do is when you click the "View PDF" button it will open the PDF file directly to the iBooks.

Here's my code for saving the file:

        currentURL = @"http://weblink.com/folder1/folder2/file.pdf";
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];

        NSURLConnection *conn = [[NSURLConnection alloc] init];
    (void)[conn initWithRequest:request delegate:self];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        data = [NSData dataWithContentsOfURL:[NSURL URLWithString:currentURL]];
        dispatch_sync(dispatch_get_main_queue(), ^{
        });
        resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

        filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"];
        [data writeToFile:filePath atomically:YES];
    });

And here's what I found from the internet for opening the file to iBooks (I put this code on button click):

    NSString *path = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:path];
    UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc] init];

    docController = [UIDocumentInteractionController interactionControllerWithURL:url];
    docController.delegate = self;

The app is crashing with that code and this message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
Ramiro
  • 229
  • 3
  • 14
  • 1
    Post the error. When ever you ask a question about an error, you must also post the full error message as well as point out the exact line of code causing the error. BTW - the use of `UIDocumentInteractionController` does not open the file in iBooks. It gives the user the ability to select an app they wish to use for the file. iBooks will be one of the options for a PDF file. – rmaddy May 14 '13 at 02:28
  • Why do you download the file twice? Once with an `NSURLConnection` and again using `NSData dataWithContentsOfURL:`. Use one or the other, not both. – rmaddy May 14 '13 at 02:29
  • And you can't save a file to the app's resource bundle. That is read-only. Save the file elsewhere in the app's sandbox. – rmaddy May 14 '13 at 02:30
  • @rmaddy there you go i edited my post. Where should I be saving the downloaded file? – Ramiro May 14 '13 at 03:09
  • 1
    The error is pretty clear. Your `path` variable is `nil`. This is because you didn't save the file in the resource bundle. Use the same path for both saving and loading. – rmaddy May 14 '13 at 03:12
  • @rmaddy tried it already, the error is gone but nothing's happening. – Ramiro May 14 '13 at 03:19

3 Answers3

0

Compare filePath when you write with path when you read. They look different to me so you probably have an invalid/nil reference in the button click code.

Gary
  • 5,642
  • 1
  • 21
  • 41
0

Try this for saving the pdf to documents folder

- (NSString *)applicationDocumentsDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

//Inside your block

filePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"myPDF.pdf"];
NSLog(@"PDF path: %@",filePath);
[data writeToFile:filePath atomically:YES];

And for opening pdf in iBooks user UIDocumentInteractionController, see this for a tutorial.

Satheesh
  • 10,998
  • 6
  • 50
  • 93
  • I tried what is written in the tutorial but I am having this error '-[UIPopoverController dealloc] reached while popover is still visible.' ... and Also what is the use of the boolean isValid? – Ramiro May 14 '13 at 06:19
  • Popover is being released before it is hided. Check whether your popover object is autoreleased or released. – Satheesh May 14 '13 at 06:32
  • I didn't declare any Popover, I'm on ARC. – Ramiro May 14 '13 at 06:40
  • Are you sure because without a popover this error would not come. – Satheesh May 14 '13 at 06:44
  • I have this 'BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];' I only followed what is in the tutorial. – Ramiro May 14 '13 at 06:49
  • See this http://stackoverflow.com/questions/4957980/how-to-check-if-uidocumentinteractioncontroller-will-fail-to-open-document-due-t – Satheesh May 14 '13 at 09:07
0
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-bookss://%@",self.pathToFile]]];

this does open a local pdf to Ibooks. 'itms-bookss' not 'itms-books' if it is already located in your Ibooks app.

other wise it looks like to add the file to iBooks is to use the not very customizable UIDocumentInteractionController.

Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
Pascale Beaulac
  • 889
  • 10
  • 28