Display Local HTML file from documents directory in a UIWebView on iPhone

别来无恙 提交于 2019-12-12 22:11:18

问题


How can I display a HTML file from the Documents directory in my app? Below is the code that I'm using and nothing is showing up in my UIWebView

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"Notes.html"];

    NSURL *url = [NSURL URLWithString:filePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [notesListWeb loadRequest:request];

Why is this not working out?

I've found other answers but they aren't from the documents directory.

Your help is greatly appreciated.


回答1:


The proper way to create an NSURL object with a file path is:

NSURL *url = [NSURL fileURLWithPath:filePath];

You should also change how you create your filePath:

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Notes.html"];

Also keep in mind that filenames are case sensitive on a real iOS device. Is the file really named Notes.html and notes.html?



来源:https://stackoverflow.com/questions/12984502/display-local-html-file-from-documents-directory-in-a-uiwebview-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!