UIWebView的使用

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.创建远程NSURL对象;指定请求的url
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    //2.创建NSURLRequest对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //2.1 设置代理
    self.webView.delegate = self;
    //3.使用webView对象加载请求
    [self.webView loadRequest:request];
}

//开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
//成功加载完毕
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
//失败返回
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    NSLog(@"请求百度主页失败:%@", error.userInfo);
}

@end

//在Info.plist中添加下面代码
Allow Arbitrary Loads    YES

你可能感兴趣的:(UIWebView的使用)