iOS笔记----网页控件WebView
2016-04-28 19:50
316 查看
What is this?
WebView控件可以加载本地HTML代码或者网络资源使用方法
加载本地HTML文件(NSString)
为同步加载方式#pragma mark - *********** 加载本地HTML文件(NSString) - (IBAction)testLoadHTMLString:(id)sender { NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; NSError *error = nil; NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&error]; if(error == nil){ [self.webView loadHTMLString:html baseURL:bundleUrl]; } }
加载本地HTML文件(NSData)
为同步加载方式#pragma mark - *********** 加载本地HTML文件(NSData) - (IBAction)testLoadData:(id)sender { NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; NSError *error = nil; NSData *htmlData = [[NSData alloc] initWithContentsOfFile:htmlPath]; if(error == nil){ [self.webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:bundleUrl]; } }
请求网址
为异加载方式#pragma mark - *********** 请求网址 - (IBAction)testLoadRequest:(id)sender { NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request]; self.webView.delegate = self; }
异步加载本地HTML
#pragma mark - *********** 请求网址 - (IBAction)testLoadLocalHttp:(id)sender { NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]; }
UIWebViewDelegate委托协定方法
WebView 将要加载页面-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ return YES; }
WebView 开始加载页面
// WebView 开始加载页面 -(void)webViewDidStartLoad:(UIWebView *)webView{ // TODO }
WebView 页面加载完成
-(void)webViewDidFinishLoad:(UIWebView *)webView{ NSLog(@"%@", [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]); }
WebView 页面加载失败
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ // TODO }
相关文章推荐
- Unity3D中世界坐标转换到NGUI坐标
- Unity世界坐标与屏幕坐标之间如何转换
- Unity ScreentoWorldPoint中的z坐标说明
- Unity3D的几种坐标系
- Android 解决android imeoptions 失效的问题
- Android中使用多张图片组成动画 AnimationDrawable
- Android Studio教程从入门到精通
- Android手机开启开发者模式
- Android相机开发中遇到的一些问题
- IOS开发-免证书调试(xcode7及以上)
- Android——Intent 相关
- Android6.0 运行时权限
- IOS 消息机制(NSNotificationCenter)
- Android项目开发填坑记-9patchPng报错
- Termios
- Android项目开发填坑记-9patchPng报错
- Android——Activity四种启动模式
- oc开发笔记4 ios 声像图 波形和频谱
- iOS工程引入ios-charts-master(cocoapod导入)
- 仿微信联系人列表字母侧滑控件