ios--UIwebView加载本地文件(支持显示图片)

来源:互联网 发布:知乎 讲师 翻译 编辑:程序博客网 时间:2024/05/21 08:01

1,本地的html文件一定要放到工程文件的根目录

2,html代码中的图片路径一定要是相对路径

3,下面是用UIWebView调用本地文件的方法


方法一:

- (void)viewDidLoad

{

    [super viewDidLoad];

    webView.backgroundColor = [UIColorclearColor];

    webView.scalesPageToFit =YES;

    webView.delegate =self;

    NSString *basePath = [[NSBundlemainBundle]bundlePath];

    NSString *helpHtmlPath = [basePath stringByAppendingPathComponent:@"jsIOS.html"];

    NSURL *url = [NSURLfileURLWithPath:helpHtmlPath];

    [webViewloadRequest:[NSURLRequestrequestWithURL:url]];

}


方法二:

 

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"html"];

    NSURL *url = [NSURL fileURLWithPath:filePath];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [myWebView loadRequest:request];

    [self.view addSubview:myWebView];


方法三:


 NSString
 *filePath = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"html"];

 NSString *htmlString = [NSString stringWithContentsOfFile:filePathencoding:NSUTF8StringEncoding error:nil]

 [myWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];




0 0