0

I have a UIWebView. When a PDF link is clicked inside that UIWebView I want to open it using the UIDocumentInteractionController.

Here is my code:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *path = [[request URL] path];
NSLog(@"current path %@", path);
NSLog(@"current request url %@", [request URL]);

if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
    if([[path pathExtension] isEqualToString:@"pdf"]) {
        NSLog(@"is PDF");

        NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentDir,@"filename.pdf"];

        NSURLRequest *req = [NSURLRequest requestWithURL:[request URL]];
        [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
            if (error) {
                NSLog(@"Download Error:%@",error.description);
            }
            if (data) {
                BOOL success = [data writeToFile:filePath atomically:YES];
                NSLog(@"File is saved to %@",filePath);

                if(success) {
                    NSURL  *localUrl = [NSURL URLWithString:filePath];
                    if(localUrl) {
                        self.documentController = [UIDocumentInteractionController interactionControllerWithURL:localUrl];
                        self.documentController.delegate = self;
                        self.documentController.name = @"";
                        [self.documentController presentPreviewAnimated:YES];
                    }
                }
            }
        }];

        return NO;
    }
}

return YES;
 }

I get the error shown in the Question title when is line is executed: self.documentController = [UIDocumentInteractionController interactionControllerWithURL:localUrl];

Any ideas? It should be something very stupid but I don't see it Thanks!

Ale
  • 2,282
  • 5
  • 38
  • 67

1 Answers1

0

if you write file to sandbox try this:

NSURL* documentsUrl = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
NSURL* destinationUrl = [documentsUrl URLByAppendingPathComponent:@"yourFileName.pdf"];
Meonardo
  • 182
  • 13