UIWebView 的使用

88 篇文章 0 订阅
21 篇文章 0 订阅
今天的例子讲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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值