开发者

Trying to load various local HTML files but the same one file always shows

开发者 https://www.devze.com 2023-02-22 23:22 出处:网络
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.

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


Try to load the files with

loadHTMLString:baseURL:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号