标签:
1.网页视图的创建,加载网页内容
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[super viewDidLoad];
//网页视图的创建
UIWebView *web=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0,320, 480)];
[self.view addSubview:web];//显示
//创建NSURL对象,给出网址
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
//创建NSURLRequest对象,传递网址
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//加载网址对应的网页内容
[web loadRequest:request];
//自动识别网页内容
web.dataDetectorTypes=UIDataDetectorTypeNone;
//自动缩放
web.scalesPageToFit=YES;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2.加载HTML代码
- (void)viewDidLoad {
[super viewDidLoad];
//网页视图的创建
UIWebView *web=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:web];//显示
NSString *html=@"This is <font color=green><font size=35>HTML";
[web loadHTMLString:html baseURL:nil];
// Do any additional setup after loading the view, typically from a nib.
}
要加载HTML代码编写的内容,就要使用 loadHTMLString:方法,其语法形式:
-(void)loadHTMLString:(NSString *) baseURL:(NSURL *)baseURL;
其中,(NSString *)string用来指定字符串对象,此对象中包含了使用HTML代码编写的内容。(NSURL *)baseURL用了指定基准的URL是一个绝对的地址,一般设置为nil
加载时常用方法(ActivityIndicatorView控件)
1)加载开始调用
-(void)webViewDidStartLoad:(UIView *)webView
{
//[activityIndicatorView startAnimating];
//activityIndicatorView.hidden=NO;
}
2)加载结束
-(void)webViewDidFinishLoad:(UIView *)webView
{
//[activityIndicatorView stopAnimating];
//activityIndicatorView.hidden=YES;
}
3)长时间加载
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//
}
注意: 这些函数都在一个名为UIWebViewDelegate协议中。
标签:
原文地址:http://www.cnblogs.com/YuanYe1/p/4621125.html