尝试加载各种本地 HTML 文件,但始终显示同一个文件
我有一个应用程序,它使用表视图来显示用户可以阅读的故事列表,当他们点击特定标题时,将打开详细视图并显示故事。
首先,我让应用程序直接从网络加载故事,效果非常好。我使用了一个数组并传递特定故事网址的详细信息,并使用以下内容加载页面
[detailWebView loadRequest:[NSURLRequest requestWithURL:detailURL]];
现在我想在本地加载文件,通过搜索我发现了以下内容
[detailWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:detailURL ofType:@"html"]isDirectory:NO]]];
,它确实加载了我的本地“故事” 1' HTML 文件,但无论我的表视图中的DetailURL 传递的文件名如何,都会加载相同的 HTML 文件,该文件采用的格式为“
[bookOne addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Story One Title",@"name",
@"Story 1",@"url",nil]];
如果我传递它,则不退出程序的文件名将退出,所以我很漂亮”确保传递不同的文件名,但始终显示相同的 HTML 页面。
我尝试过重新启动等,但总是打开相同的文件,非常感谢您的帮助。
谢谢 基隆
I have an app that uses a Table View to show a list of stories a user can read and when they tap on a particular title a Detail View will open up and the story is displayed.
To begin with I had the app loading up the stories directly from the web and this worked perfectly. I used an array and to pass the details of the particular stories web address and used the following to load up the page
[detailWebView loadRequest:[NSURLRequest requestWithURL:detailURL]];
Now I want to load up files locally instead and from searching around I found the following
[detailWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:detailURL ofType:@"html"]isDirectory:NO]]];
and it does load up my local 'Story 1' HTML file but that same HTML file gets loaded up regardless of the file name being passed by detailURL in my Table View which takes the format of
[bookOne addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:@"Story One Title",@"name",
@"Story 1",@"url",nil]];
If I pass it a file name that doesn't exit the program quits so I'm pretty sure the different file names are being passed but the same HTML page always shows.
I've tried reboots etc but the same file always opens up, your help would be really appreciated.
Thanks
Kieron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试加载文件,
它可能是 NSURLRequest 缓存文件/响应。
另一种解决方案是使用
initWithURL:cachePolicy:timeoutInterval:
创建 NSURLRequest 实例,并为缓存策略传递 NSURLRequestReloadIgnoringLocalAndRemoteCacheDataTry to load the files with
it might be that NSURLRequest caches the file/response.
A different solution would to create an instance of the NSURLRequest with
initWithURL:cachePolicy:timeoutInterval:
and pass NSURLRequestReloadIgnoringLocalAndRemoteCacheData for the cachepolicy谢谢尼克,虽然 loadHTMLString 没有提供答案,但它让我以不同的方式思考问题的真正原因,并走上正确的轨道。
当在第一个示例中使用网址时,detailURL 已设置为 NSURL,当我将其更改为 NSString 时,我的代码工作正常。我不明白为什么它会导致 HTML 文件的加载行为如此奇怪,但现在一切都运行良好,不同的故事都按应有的方式加载,并且不存在任何类型的缓存问题。
Thanks Nick, while loadHTMLString didn't provide the answer it got me thinking differently and on the right track as to the real cause of the problem.
When using a web address in the first example detailURL had been set as NSURL and when I changed it to NSString my code worked fine. I don't understand why it would cause the loading of the HTML file(s) to act so weirdly but everything is running fine now, the different stories all load up as there should and there are no caching issues of any sort.