总结-UIWebView的使用-加载网页、html文件、本地文件等

来源:互联网 发布:修改telnet端口 编辑:程序博客网 时间:2024/06/09 08:12
一、概念
     UIWebView是用来加载html文件、网址、本地文件等的框架。

二、使用方法
0.初始化UIWebView
     可以用storyboard初始化,也可以代码初始化。
if (!_webView) {
       
CGRect frame = [UIScreenmainScreen].bounds;
       
_webView = [[UIWebViewalloc]initWithFrame:frame];
       
//识别webView中的类型,如有电话号码,点击直接可拨打
       
_webView.dataDetectorTypes =UIDataDetectorTypeAll;
       
_webView.delegate =self;
       
_webView.scalesPageToFit =YES;
        [
self.viewaddSubview:_webView];
    }
     初始化UIWebView时,_webView.dataDetectorTypes = UIDataDetectorTypeAll; 是识别webview中的类型,例如 当webview中有电话号码,点击号码就能直接打电话。

1.加载网页
     NSString * urlStr = [NSStringstringWithFormat:@"http://www.baidu.com"];
       NSURL * url = [NSURLURLWithString:urlStr];
       
NSURLRequest * request = [NSURLRequestrequestWithURL:url];
     [_webViewloadRequest:request];

2.加载本地html文件(可加载image)
//baseURL把项目根目录统一中项目下,加载double.html中的image, css的话,不需要写路径,直接写名字就可以。这样html中的图片就可以正常显示了
       
NSURL * baseURL = [NSURLfileURLWithPath:[NSBundlemainBundle].bundlePath];
       
       
NSString * htmlPath = [[NSBundlemainBundle]pathForResource:@"double"ofType:@"html"];
       NSString * htmlCont = [NSStringstringWithContentsOfFile:htmlPathencoding:NSUTF8StringEncodingerror:nil];
        [_webViewloadHTMLString:htmlContbaseURL:baseURL];

3.加载加载本地或者从服务器下载的文件,如txtpdfword
//获取本地文件的url
       
NSURL * fileURL = [[NSBundlemainBundle]URLForResource:@"LocalFile.txt"withExtension:nil];
       
NSURLRequest * request = [NSURLRequestrequestWithURL:fileURL];
        [_webViewloadRequest:request];





0 0
原创粉丝点击