总结-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];
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];
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"];
NSURL * baseURL = [NSURLfileURLWithPath:[NSBundlemainBundle].bundlePath];
NSString * htmlPath = [[NSBundlemainBundle]pathForResource:@"double"ofType:@"html"];
NSString * htmlCont = [NSStringstringWithContentsOfFile:htmlPathencoding:NSUTF8StringEncodingerror:nil];
[_webViewloadHTMLString:htmlContbaseURL:baseURL];
3.加载加载本地或者从服务器下载的文件,如txt,pdf,word等
//获取本地文件的url
NSURL * fileURL = [[NSBundlemainBundle]URLForResource:@"LocalFile.txt"withExtension:nil];
NSURLRequest * request = [NSURLRequestrequestWithURL:fileURL];
NSURL * fileURL = [[NSBundlemainBundle]URLForResource:@"LocalFile.txt"withExtension:nil];
NSURLRequest * request = [NSURLRequestrequestWithURL:fileURL];
[_webViewloadRequest:request];
0 0
- 总结-UIWebView的使用-加载网页、html文件、本地文件等
- UIWebView加载网页、html、本地文件、二进制方式加载本地文件
- UIWebView加载网页、文件及html的方法
- ios UIWebView 加载网页、文件、 html
- ios UIWebView 加载网页、文件、 html
- ios UIWebView 加载网页、文件、 html
- ios UIWebView 加载网页、文件、 html
- ios UIWebView 加载网页、文件、 html
- ios UIWebView 加载网页、文件、 html
- iOS UIWebView 加载网页、文件、 html
- ios UIWebView 加载网页、文件、 html
- ios UIWebView 加载网页、文件、 html
- UIWebView 加载网页数据(网页,网页片段,本地文件)
- 使用UIWebView加载网页
- 加载本地文件到UIWebView的方法
- 【IOS 开发】UIWebView 加载本地文件,多层的网页目录结构失败分析和解决,以及demo
- UIWebView加载本地文件
- uiwebview加载各种本地文件
- node学习-assert测试
- Minimum Depth of Binary Tree(求二叉树的最小深度)
- MyEclipse-blue2014怎样破解?
- NSOperation与performSelectorOnMainThread
- 使用Git将项目push至github
- 总结-UIWebView的使用-加载网页、html文件、本地文件等
- mac安装mysql数据库后忘记密码
- VLC的ACtiveX插件使用方法
- 聊聊设计模式 — 模板方法模式
- NOIp2009 最优贸易
- 新手入门一篇就够:从零开发移动端IM
- 【C/C++】error: a label can only be part of a statement and a declaration is not a statement
- C#之WinForm基础 使用代码让textbox换行
- Inner class