创建webView
UIWebView *myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height)];
myWebView.delegate=self;
[myWebView setScalesPageToFit:YES]; //自动缩放页面以适应屏幕
NSURLRequest*request=[NSURLRequest requestWithURL:[NSURLURLWithString:urlStr]];
[myWebView loadRequest:request];
[self.view addSubview:myWebView];
代理的方法
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"erros is %@",error);
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"hello");
}
<pre name="code" class="objc">-(void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@"Loading webViewDidStartLoad method");
}
注意一点 url字符串中如果包含了非法字符或者中文,要使用
[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
编码一下,否则会报错:Domain=WebKitErrorDomain Code=101 "The operation couldn’t be completed.