首先创建UIWebView,这个不难,设置frame,然后添加到self.view上即可:
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
[self.view addSubview:webView];
UIWebView加载网络地址
用UIWebView来加载网络地址,那我的博客:http://blog.csdn.net/dylan_lwb_
来举例。代码如下:
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blog.csdn.net/dylan_lwb_"]];
[self.view addSubview:webView];
[webView loadRequest:request];
UIWebView加载本地资源
通过NSURLRequest加载
NSString *path = [[NSBundle mainBundle] pathForResource:@"swift" ofType:@"html"];
NSURL* url = [NSURL fileURLWithPath:path];//创建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
[webView loadRequest:request];//加载
通过NSString加载
UIWebView 还支持将一个NSString对象作为源来加载。你可以为其提供一个基础URL,来指导UIWebView对象如何跟随链接和加载远程资源:
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:path]];
自动检测电话
设置这个属性,如果在加载的网页中遇到电话号码,直接单击就可以拨打,非常方便:
webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;//自动检测网页上的电话号码,单击可以拨打
设置代理和代理方法
UIWebView有五个代理方法,我们可以在不同的代理方法中,完成不同的需求。
//设置代理
webView.delegate = self;
//代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//返回YES,进行加载。通过UIWebViewNavigationType可以得到请求发起的原因
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
//开始加载,可以加上风火轮(也叫菊花)
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//完成加载
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//加载出错
}
创建加载他、停止、后退、前进按钮
创建四个按钮,分别是加载按钮、停止按钮、返回按钮、前进按钮:
NSArray *array = [NSArray arrayWithObjects:@"加载",@"停止",@"返回",@"前进", nil];
int far = (SCREEN.width - 200)/5;
for (int i = 0; i < array.count; i++) {
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake( (i + 1) * far + 50 * i, SCREEN.height - 60, 50, 40);
button.tag = 200 + i;
[button addTarget:self action:@selector(actionClick:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
[button setTitle:array[i] forState:UIControlStateNormal];
[self.view addSubview:button];
}
前进后退
给四个按钮绑定事件,根据tag值来区分每一个点击事件:
- (void)actionClick:(UIButton *)button
{
switch (button.tag) {
case 200:
{
[self.webView reload];//加载
}
break;
case 201:
{
[self.webView stopLoading];//停止加载
}
break;
case 202:
{
[self.webView goBack];//返回
}
break;
case 203:
{
[self.webView goForward];//前进
}
break;
default:
break;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-09 04:35:45