Reputation: 5970
I am trying to display a pdf in a UIWebview
but I am getting an error. Can anyone help me as to why this is not working? The error is:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
Each of the strings are correct except path which seems to be of 0 length and I am not sure why.
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [docPath objectAtIndex:0];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 700, 500)];
NSString *path = [[NSBundle mainBundle] pathForResource:@"pdfDoc" ofType:@"pdf" inDirectory:documentDirectory];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
Upvotes: 0
Views: 148
Reputation: 119031
You can't do this:
NSString *path = [[NSBundle mainBundle] pathForResource:@"pdfDoc" ofType:@"pdf" inDirectory:documentDirectory];
because NSBundle
can only get paths for files in the bundle, not in the documents directory (so it returns nil
and you get an error).
If the file is in the bundle, search for it only by name. If the file is in the documents folder, get the path to the folder and append the name (stringByAppendingPathComponent:
). Then create the file URL with that path.
Upvotes: 1