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'