1

I am display local pdf file in UIWebView but it shows exception in path here is my code

     NSString*fileName=content_Source;
     NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pdf"];
     NSURL *url = [NSURL fileURLWithPath:path];
     NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView setScalesPageToFit:YES];
    [webView loadRequest:request]
Ali Sajad
  • 59
  • 1
  • 3
  • 10

4 Answers4

6

You must be using the file name extension while setting the file name. So make sure the extension will not be used in file name if it's setting along with Type (like ofType:@"pdf").

Here's my code:

UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor redColor];
NSString*fileName= @"About_Downloads";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView];

There's a better way to show PDFs:

UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
dc.delegate = self; 
[dc presentPreviewAnimated:YES];

Make sure use the delegate along with that code to work it properly.

(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Harjot Singh
  • 6,767
  • 2
  • 38
  • 34
1

first validate that string and the assign to URL like bellow..

   NSString *strURLPath = [self trimString: path];
   NSURL *url = [NSURL fileURLWithPath:strURLPath];

use my bellow method...

-(NSString*) trimString:(NSString *)theString {
    if (theString != [NSNull null])
        theString = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return theString;
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • same erros @ParasJoshi -[NSURL initFileURLWithPath:]: nil string parameter' – Ali Sajad Apr 16 '13 at 06:14
  • First check what you get in content_Source variable??? NSLog(@"\n\n content_Source ==>> %@",content_Source); – Paras Joshi Apr 16 '13 at 06:15
  • if i am giving direct name then it is working NSString *path = [[NSBundle mainBundle] pathForResource:@"rglucagon-pi" ofType:@"pdf"]; – Ali Sajad Apr 16 '13 at 06:17
  • yes exactly i want to say you that and for that i toild you first chcek what u get in content_Source variable... check that... – Paras Joshi Apr 16 '13 at 06:18
  • here in your content_Source variable is nil and so you get this error first assign that name to this variable like this content_Source = @"rglucagon-pi"; and then retain it like [content_Source retain]; and then assign it to the file name and it will worked.. :) – Paras Joshi Apr 16 '13 at 06:23
  • @ParasJoshi: Please help me with this http://stackoverflow.com/questions/21874538/failed-to-download-and-open-a-pdf-file-with-error-failed-to-find-pdf-header-in-i – Manthan Feb 19 '14 at 08:14
1

It is working fine with the given code problem was that in my file Name i was giving also the type like ali.pdf they type pdf so it get exception it worked fine now when i use filename like following

 NSString*fileName=@"ali";

Instead of

    NSString*fileName=@"ali.pdf";
Ali Sajad
  • 59
  • 1
  • 3
  • 10
0
CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize screenSize = rect.size;
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"pdf" ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [webView loadRequest:request];

    [self.view addSubview:webView];
TharakaNirmana
  • 10,237
  • 8
  • 50
  • 69