gpt4 book ai didi

iphone - 如何在UIWebView中显示本地pdf文件?

转载 作者:行者123 更新时间:2023-12-03 21:03:56 24 4
gpt4 key购买 nike

我是 Objective-C 和 iOS 开发的新手。我无法在 UIWebView 中显示本地 pdf 文件。我正在设置一个数组来保存文件名,然后将它们显示在 UIWebView 中。任何帮助将不胜感激!

来自 MasterViewController.m:(设置 pdf 文件名数组)

[mCommands addObject:[[NSDictionary alloc]
initWithObjectsAndKeys:@"Break",@"name",@"cmd_break.pdf",@"url",nil]];
[mCommands addObject:[[NSDictionary alloc]
initWithObjectsAndKeys:@"Close",@"name",@"cmd_close.pdf",@"url",nil]];

来自DetailViewController.m:(在UIWebView中显示pdf文件)

NSString *filename = [self.detailItem objectForKey:@"name"];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];
NSURL *detailURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:detailURL];
[self.detailWebView loadRequest:request];

最佳答案

您将 pdf 加载到 View 中的代码正确。

但是文件路径可能是错误的。当您使用时:

[[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];

它只需要文件名,它会附加类型字符串。所以你可能会得到一个以cmd_break.pdf.pdf

验证这一点:

NSString *filename = [self.detailItem objectForKey:@"url"];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"pdf"];
NSLog(@"%@", path);

是的,如 Richard J. Ross III指出。您在字典中使用了错误的键。 “name”键将为您返回“Break”,而“url”键将为您提供“cmd_break.pdf”。

关于iphone - 如何在UIWebView中显示本地pdf文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9555543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com