今天的例子讲UIWebView 内置的浏览器 方便我们的使用
#import "ViewController.h"
@interface ViewController ()<UIWebViewDelegate>
{
UIWebView *_myWebView;
UIImageView *_viewBar;
UIActivityIndicatorView *_activityIndicatorView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *str = @"http://www.baidu.com";
NSURL *url = [NSURL URLWithString:str];
//直接用浏览器打开,比较省事
//[[UIApplication sharedApplication]openURL:url];
[self showUIWebView:url];
[self createBarView];
}
-(void)showUIWebView:(NSURL *)url
{
_myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, BAR_HIGHT, MAIN_SIZE.width, MAIN_SIZE.height-BAR_HIGHT)];
_myWebView.backgroundColor = [UIColor whiteColor];
_myWebView.scrollView.bounces = YES;
_myWebView.userInteractionEnabled = YES;
_myWebView.delegate = self;
//打开请求页面
[_myWebView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:_myWebView];
[self showLoading];//
}
-(void)createBarView{
//背景图
_viewBar = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MAIN_SIZE.width, BAR_HIGHT)];
_viewBar.image = [UIImage imageNamed:@"bar_bg"];
//设置可接收事件
_viewBar.userInteractionEnabled = YES;
//建立数组模型
NSArray *imageNames = @[@"back_n",@"left_n",@"right_n",@"refresh_n"];
NSArray *selectedNames = @[@"back_h",@"left_h",@"right_h",@"refresh_h"];
for (NSInteger i = 0; i < 4; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat itemLength = MAIN_SIZE.width/4;
button.frame = CGRectMake(itemLength*i, 0, itemLength, BAR_HIGHT);
//设置图片
NSString *imageName = [NSString stringWithFormat:@"%@.png",imageNames[i]];
NSString *selectedImageName = [NSString stringWithFormat:@"%@.png",selectedNames[i]];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 100 + i;
[_viewBar addSubview:button];
}
[self.view addSubview:_viewBar];
}
//菊花
-(void)showLoading{
_activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[_activityIndicatorView setCenter:self.view.center];
[_activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
_activityIndicatorView.hidesWhenStopped = YES;
[_activityIndicatorView startAnimating];
[self.view addSubview:_activityIndicatorView];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
DEBUGLOG(@"DidStartLoa");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[_activityIndicatorView stopAnimating];
DEBUGLOG(@"DidFinishLoad");
//1.获取当前页面的url查看用户干什么了
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSLog(@"currentURL = %@",currentURL);
//2.获取当前界面的title
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"title = %@",title);
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[self showMessage:@"加载失败,请检查网络"];
DEBUGLOG(@"didFailLoad");
}
#pragma mark -响应事件-
-(void)btnClick:(UIButton *)btn{
switch (btn.tag) {
case 100:
[_myWebView stopLoading];
break;
case 101:
[_myWebView goBack];
break;
case 102:
[_myWebView goForward];
break;
case 103:
[_myWebView reload];
break;
default:
break;
}
}
#pragma mark -消息提示-
-(void)showMessage:(NSString *)msg{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UIWebView 的使用
最新推荐文章于 2018-03-27 10:52:52 发布