#import <UIKit/UIKit.h>
@interface UIWebViewController : UIViewController<UIWebViewDelegate>
@end
#import "UIWebViewController.h"
@interface UIWebViewController ()
@end
@implementation UIWebViewController
UIWebView * webView;
UIActivityIndicatorView *activityIndicator;
- (void)viewDidLoad {
[super viewDidLoad];
// Xcode 7, 涉及到了访问http资源,测试时碰到如下报错:
// App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.
// 解决方法:打开工程主目录下info.plist
// 增加属性字典 App Transport Security Settings
// 这个属性下增加节点 Allow Arbitrary Loads, value 为 YES
CGRect rect=[[UIScreen mainScreen]applicationFrame];
rect=self.view.frame;
webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 120, rect.size.width, rect.size.height)];
//加载网页
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
//加载本地html
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [resourcePath stringByAppendingPathComponent:@"serviceAgreement.html"];
NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
//自动对页面进行缩放以适应屏幕
webView.scalesPageToFit = YES;
//自动检测网页上的电话号码,单击可以拨打
webView.detectsPhoneNumbers = YES;
//为webView添加背景图片
webView.backgroundColor=[UIColor clearColor];
webView.opaque=NO;//这句话很重要 webView是否是不透明的 NO为透明 在webView下添加个imageView展示图片就可以了
//UIWebView类内部会管理浏览器的导航动作
//[webView goBack];//后退
//[webView goForward];//前进
//[webView reload];//重载
//[webView stopLoading];//取消载入内容
//实现协议
webView.delegate=self;
[self.view addSubview:webView];
}
- (void) webViewDidStartLoad:(UIWebView *)webView
{
//创建UIActivityIndicatorView背底半透明View
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[view setTag:108];
[view setBackgroundColor:[UIColor blackColor]];
[view setAlpha:0.5];
[self.view addSubview:view];
activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40, 40)];
[activityIndicator setCenter:view.center];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[view addSubview:activityIndicator];
[activityIndicator startAnimating];
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
UIView *view = (UIView*)[self.view viewWithTag:108];
[view removeFromSuperview];
NSLog(@"webViewDidFinishLoad");
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[activityIndicator stopAnimating];
UIView *view = (UIView*)[self.view viewWithTag:108];
[view removeFromSuperview];
NSLog(@"didFailLoadWithError:%@", error);
}
@end