WebView的使用
使用UIWebView可以实现一个内置的浏览器,而且可以直接将这个浏览器内嵌到应用的任意位置
UIWebView直接集成了UIView,一般不可以与用户交互
获取UIWebView对象后,可配置如下常用属性和方法
@property(nonatomic) BOOL scalesPageToFit;//该属性控制是否缩放网页以适应该控件
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes NS_AVAILABLE_IOS(3_0);//该属性支持以下枚举
- UIDataDetectorTypePhoneNumber //自动检测网页上的电话号码,点击会拨号
- UIDataDetectorTypeLink //自动检测网页上的超级链接
- UIDataDetectorTypeAddress //自动检测网页上的地址
- UIDataDetectorTypeCalendarEvent //自动检测网页上的日历事件
- UIDataDetectorTypeNone //不检测网页上的任何内容
- UIDataDetectorTypeAll //自动检测网页上所有特殊内容
- (void)loadRequest:(NSURLRequest *)request;//加载显示URL网页请求
- (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;//加载HTML
@property (nullable, nonatomic, readonly, strong) NSURLRequest *request;//URL请求
- (void)reload;//重新加载
- (void)stopLoading; //停止加载
- (void)goBack; //网页后退
- (void)goForward; //网页前进
-
当UIWebView加载网页时,程序可为UIWebView设置一个delegate委托,该委托必须实现<UIWebViewDelegate>协议,该协议包括如下方法:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;//当UIWebView将要加载指定的URL请求时,激发该方法
- (void)webViewDidStartLoad:(UIWebView *)webView;//当开始加载时激发
- (void)webViewDidFinishLoad:(UIWebView *)webView;//当完成加载时激发
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error;//当加载错误时激发
通过下面实例代码来实现一个 迷你浏览器
@interface ViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UITextField*addressBar;
@end
@implementationViewController{
UIActivityIndicatorView *_activityIndizator;
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置自动缩放网页以适应该控件
self.webView.scalesPageToFit = YES;
//为该控件设置委托
//因为使用该控制器对象作为UIWebView控件的委托对象,所以该控制器类需要实现<UIWebViewDelegate>协议
self.webView.delegate = self;
//创建UIActivityIndicatorView控件,用来显示网页加载中
_activityIndizator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
//将UIActivityIndicatorView显示在当前View的中央
[_activityIndizator setCenter:self.view.center];
//设置样式
_activityIndizator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
//将_activityIndizator添加到View 并隐藏
[self.view addSubview:_activityIndizator];
_activityIndizator.hidden = YES;
}
//点击前往打开网页
- (IBAction)goButton:(id)sender {
[self.addressBar resignFirstResponder];
//获取用户输入网址
NSString *addressStr = self.addressBar.text;
//如果用户输入的地址不包括http:// 则为用户添加http://
if (![addressStr hasPrefix:@"http://"]) {
addressStr = [NSString stringWithFormat:@"http://%@",addressStr];
self.addressBar.text = addressStr;
}
//创建URL请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:addressStr]];
//加载指定URL对应的网页
[self.webView loadRequest:request];
}
//实现后退方法
- (IBAction)goBackButton:(id)sender {
if (self.webView.canGoBack) {
[self.webView goBack];
}
}
//实现<UIWebViewDelegate>协议中的方法
//当WebView开始加载时
- (void)webViewDidStartLoad:(UIWebView *)webView{
//显示_activityIndizator 并开始转动
_activityIndizator.hidden = NO;
[_activityIndizator startAnimating];
}
//当WebView完成加载时
- (void)webViewDidFinishLoad:(UIWebView *)webView{
_activityIndizator.hidden = YES;
[_activityIndizator stopAnimating];
}
//当WebView加载失败时
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
//当加载错误时,用UIAlertController来显示错误信息
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"错误" message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAlert = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnullaction) {
}];
[alert addAction:cancelAlert];
//显示Alert
[self presentViewController:alert animated:YES completion:^{
_activityIndizator.hidden = YES;
[_activityIndizator stopAnimating];
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最终通过代码可实现如下效果
如果出现下面这个提示框 则说明网页加载失败
错误原因是因为,默认在iOS9 后,苹果将原http协议改成了https协议,使用 TLS1.2 SSL加密请求数据。我们只需要为Info.plist如下字段即可