webView的基本用法

#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate,UIWebViewDelegate>
{
    UIWebView *_webView;
    UISearchBar *_searchBar;
    UIToolbar *toolBar;//底部工具栏
    UIBarButtonItem *backButton;//回退
    UIBarButtonItem *forwardButton;//前进
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layout];
}
-(void)layout{
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, 375, 44)];
    //遵守协议
    _searchBar.delegate = self;
    //添加浏览器
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, 375, 559)];
    _webView.delegate = self;
    _webView.backgroundColor = [UIColor grayColor];
  
    
    [self.view addSubview:_webView];
    [self.view addSubview:_searchBar];
    //添加工具栏
    toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 623, 375, 44)];
    //设置工具栏按钮(回退和前进)
    backButton = [[UIBarButtonItem alloc]initWithTitle:@"⬅️" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    forwardButton = [[UIBarButtonItem alloc]initWithTitle:@"➡️" style:UIBarButtonItemStyleDone target:self action:@selector(forward)];

    UIBarButtonItem *spacingButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    toolBar.items = @[backButton,spacingButton,forwardButton];
    [self.view addSubview:toolBar];
    
    
}
-(void)request:(NSString*)urlStr{
    //创建url
    NSURL *url;
    //如果file://开头的字符串则加载bundle中的文件
    if ([urlStr hasPrefix:@"file://"]) {
        //1.获取文件名位置
        NSRange range = [urlStr rangeOfString:@"file://"];
        NSString * fileName = [urlStr substringFromIndex:range.length];
        NSLog(@"%@",fileName);
        //2.获取文件位置
        url = [[NSBundle mainBundle]URLForResource:fileName withExtension:nil];
    }else if ([urlStr hasPrefix:@"http://"]){
        url = [NSURL URLWithString:urlStr];
    }else{//如果不符合任何协议则进行百度搜索
        urlStr = [NSString stringWithFormat:@"https://www.baidu.com/s?wd=%@",urlStr];
        //url编码
        urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        url = [NSURL URLWithString:urlStr];
    }
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
}
#pragma mark searchBar代理方法
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
//    NSLog(@"搜索:%@",searchBar.text);
    [self request:searchBar.text];
}
#pragma mark webview代理方法
#pragma mark 开始加载
-(void)webViewDidStartLoad:(UIWebView *)webView{
   
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
#pragma mark 加载完毕
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    //显示当前加载的url
    NSLog( @"%@",webView.request.URL);
    _searchBar.text = [NSString stringWithFormat:@"%@",webView.request.URL];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
//回退
-(void)back{
    [_webView goBack];
}
//前进
-(void)forward{
    [_webView goForward];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


你可能感兴趣的:(webView的基本用法)