webView简单使用
2015-03-04 本文已影响171人
英贰与我
webView是ios系统内置的浏览器控件,它可以用来浏览网页,打开文档等,能够加载html/htm,pdf,docx,txt等格式;
webView加载网页是很简单的,三步就可以搞定:
1.定位资源
NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];
2.创建网络请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
3.用webView加载页面
[self.webView loadRequest:request];
这个就是webView最简单的使用,下面附加一个我自己的简单工程,让webView的加载更加优化;
1.我们创建一个工程,用sroeytBoard在viewControllView上添加一个searchBar控件,一个webView,两个按钮,分别是返回键和前进键,并且做逻辑处理(签订代理协议等等)如图所示


2.实现
1>加载资源

2>搜索框代理方法.在按下搜索键时调用加载资源的方法

3>让前进和后退按钮的状态随着webView的加载状态变化,用webView的代理方法,当加载完成的时候来判断当前网页是否可以前进或后退

4>在ViewDidLoad中调用加载资源,URL为百度搜索

这样,一个简单地webView浏览器就做出来了,可以将代码敲在工程中尝试哦;