创建一个WebView的基本方法

#import<UIKit/UIKit.h>

@interface WebViewController : UIViewController

@end


#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate>

{

UIWebView *webView;

UIActivityIndicatorView *activity;

}

@end

@implementation WebViewController

- (void)viewDidLoad {

[self _creatImageView];

}

- (void)_creatImageView{

//    NSLog(@"%@",_urlStr);

webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height)];

webView.backgroundColor = [UIColor whiteColor];

NSString *webStr = @"https://qr.alipay.com/apx08944gtboitcgqa8vjf8";

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:webStr]];

[self.view addSubview:webView];

webView.delegate = self;

[webView loadRequest:request];

}

//网页开始加载的时候调用

- (void )webViewDidStartLoad:(UIWebView  *)webView{

NSLog(@"webViewDidStartLoad");

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

[view setTag:108];

[view setBackgroundColor:[UIColor blackColor]];

[view setAlpha:0.5];

[self.view addSubview:view];

activity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];

[activity setCenter:view.center];

[activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];

[view addSubview:activity];

[activity startAnimating];

}

//网页加载完成的时候调用

- (void )webViewDidFinishLoad:(UIWebView  *)webView {

[activity stopAnimating];

UIView *view = (UIView *)[self.view viewWithTag:108];

[view removeFromSuperview];

NSLog(@"webViewDidFinishLoad");

}

//网页加载错误的时候调用

- (void)webView:(UIWebView *)webView  didFailLoadWithError:(NSError *)error {

[activity stopAnimating];

UIView *view = (UIView *)[self.view viewWithTag:108];

[view removeFromSuperview];

NSLog(@"didFailLoadWithError:%@",error);

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络是否连接正确" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alertView show];

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容