【问题标题】:Display local pdf file in UIWebView in iphone在 iphone 的 UIWebView 中显示本地 pdf 文件
【发布时间】:2013-04-16 05:27:34
【问题描述】:

我在 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]

【问题讨论】:

  • 什么异常错误..?
  • @NitinGohel 这是由于未捕获的异常 'NSInvalidArgumentException' 导致的异常应用程序,原因:'*** -[NSURL initFileURLWithPath:]: nil string parameter'

标签: iphone uiwebview


【解决方案1】:

您必须在设置文件名时使用文件扩展名。因此,请确保扩展名不会在文件名中使用,如果它与类型一起设置(如 ofType:@"pdf")。

这是我的代码:

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

有一种更好的方式来显示 PDF:

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

确保将委托与该代码一起使用以使其正常工作。

(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}

【讨论】:

    【解决方案2】:

    首先验证该字符串并分配给如下所示的 URL。

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

    用我下面的方法...

    -(NSString*) trimString:(NSString *)theString {
        if (theString != [NSNull null])
            theString = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        return theString;
    }
    

    【讨论】:

    • 同样的错误@ParasJoshi -[NSURL initFileURLWithPath:]: nil string parameter'
    • 首先检查你在 content_Source 变量中得到了什么??? NSLog(@"\n\n content_Source ==>> %@",content_Source);
    • 如果我给出直接名称,那么它正在工作 NSString *path = [[NSBundle mainBundle] pathForResource:@"rglucagon-pi" ofType:@"pdf"];
    • 是的,我确实想说你,为此我告诉你首先检查你在 content_Source 变量中得到了什么......检查......
    • 在你的 content_Source 变量中是 nil ,所以你得到这个错误首先将这个名称分配给这个变量,比如这个 content_Source = @"rglucagon-pi";然后像 [content_Source 保留] 一样保留它;然后将其分配给文件名,它将起作用.. :)
    【解决方案3】:

    在给定的代码问题上工作正常,问题是在我的文件名中,我也给出了像 ali.pdf 这样的类型,他们输入了 pdf,所以当我使用如下文件名时它会出现异常,现在它工作正常

     NSString*fileName=@"ali";
    

    代替

        NSString*fileName=@"ali.pdf";
    

    【讨论】:

      【解决方案4】:
      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];
      

      【讨论】:

        猜你喜欢
        • 2012-03-22
        • 1970-01-01
        • 1970-01-01
        • 2012-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-23
        • 2017-11-26
        相关资源
        最近更新 更多