简体   繁体   English

在 iphone 中的 UIWebView 中显示本地 pdf 文件

[英]Display local pdf file in UIWebView in iphone

I am display local pdf file in UIWebView but it shows exception in path here is my code我在 UIWebView 中显示本地 pdf 文件,但它在路径中显示异常,这是我的代码

     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]

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").因此,如果扩展名与类型一起设置(如 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:有一种更好的方式来显示 PDF:

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;
}

first validate that string and the assign to URL like bellow..首先验证该字符串并分配给 URL,如下所示..

   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;
}

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对于给定的代码问题,它工作正常,因为在我的文件名中,我还提供了类似 ali.pdf 的类型,他们键入 pdf 所以它得到异常,现在当我使用如下文件名时它工作正常

 NSString*fileName=@"ali";

Instead of代替

    NSString*fileName=@"ali.pdf";
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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM