【问题标题】:Error opening pdf in UIWebview在 UIWebview 中打开 pdf 时出错
【发布时间】:2014-01-27 16:00:30
【问题描述】:

我正在尝试在 UIWebview 中显示 pdf,但出现错误。谁能帮我解释为什么这不起作用?错误是:

* 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'* -[NSURL initFileURLWithPath:]: nil string parameter'


每个字符串都是正确的,除了 path 似乎是 0 长度,我不知道为什么。

NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES);
NSString *documentDirectory = [docPath objectAtIndex:0];

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 700, 500)];

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

[self.view addSubview:webView];

【问题讨论】:

    标签: ios pdf uiwebview


    【解决方案1】:

    你不能这样做:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"pdfDoc" ofType:@"pdf" inDirectory:documentDirectory];
    

    因为NSBundle 只能获取包中文件的路径,而不是文档目录中的路径(因此它返回nil 并且您会收到错误消息)。

    如果文件在包中,则仅按名称搜索。如果文件位于文档文件夹中,请获取文件夹的路径并附加名称 (stringByAppendingPathComponent:)。然后使用该路径创建文件 URL。

    【讨论】:

    • 太棒了 - 完成了这项工作。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多