Kevin Jack
Kevin Jack

Reputation: 15

display the PDF file in webview

i am doing one application.In that i am displaying the pdf file in webview like below

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:@"KeyToSelectedFile"]];
NSString *filename1=[fileName stringByAppendingPathComponent:s1];

NSLog(@"%@",filename1);
NSURL *url = [NSURL fileURLWithPath:filename1];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
web=[[UIWebView alloc]initWithFrame:CGRectMake(0,0, 1020, 748)];
web.delegate=self;
[web loadRequest:request];
 web.backgroundColor=[UIColor whiteColor];
[self.view addSubview:web];

But when i click open on first time it will be crashing the app and from next time onwards it's opening correctly.And at first time o got the error like

dyld: fast lazy binding from unknown image

So please tell me how to display pdf in uiwebview without crashing.

Upvotes: 0

Views: 268

Answers (4)

Vedchi
Vedchi

Reputation: 1210

I checked this, it is working fine, try

UIWebView * pdfWebView = [[UIWebView alloc] initWithFrame:'your Frame'];
NSArray * Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * bundlePath = [[Paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",yourFileName]];

if([[NSFileManager defaultManager] fileExistsAtPath:bundlePath])
{
    NSLog(@"Path : %@",bundlePath);
    [pdfWebView loadData:[NSData dataWithContentsOfFile:bundlePath] MIMEType:@"application/pdf" textEncodingName:nil baseURL:nil];
    [self.view addSubview:pdfWebView];
}

Upvotes: 1

Monish Bansal
Monish Bansal

Reputation: 509

This will work perfect.

UIWebView * webview = [[UIWebView alloc] initWithFrame:Frmaesize
NSString * path = [[NSBundle mainBundle] pathForResource:@"Filename" ofType:@"pdf"];
NSURL * URL = [NSURL path];
NSURLRequest * request = [NSURLRequest requestWithURL:URL];
[webview loadRequest:request];
[self.view addsubView:webview];

Upvotes: 0

βhargavḯ
βhargavḯ

Reputation: 9836

Try this way for your code,

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[default1 objectForKey:@"KeyToSelectedFile"]];
        NSString *filename1=[fileName stringByAppendingPathComponent:s1];

        web=[[UIWebView alloc]initWithFrame:CGRectMake(0,0, 1020, 748)];
        web.delegate=self;
        web.backgroundColor=[UIColor whiteColor];
        NSData *data = [NSData dataWithContentsOfFile:filename1];
        [web loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:url];
        [self.view addSubview:web];

Upvotes: 0

BhushanVU
BhushanVU

Reputation: 3455

NSData *data = [NSData dataWithContentsOfFile: filename1];
[web loadData:data MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
web.backgroundColor=[UIColor whiteColor];
[self.view addSubview:web];

Upvotes: 0

Related Questions