UIWebView 的使用
2015-09-02 11:58
351 查看
今天的例子讲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
相关文章推荐
- iOS进阶路线以及进阶书籍
- iOS 多线程与GCD
- Application windows are expected to have a root view controller at the end of application launch
- android视频缓冲进度条SeekBar有断断续续的间断效果
- iOS开发过程中的疑问
- 【Cocos2dx】中文乱码问题
- Android中ListView.getCount()与ListView.getChildCount()区别和OnScrollListener()各个参数的区别
- 把NSObject对象输出为字典
- IOS Block语法
- android sharePreference的使用
- 理解$watch ,$apply 和 $digest --- 理解数据绑定过程
- Android四大组建之Service
- Android中的长度单位详解(dp、sp、px、in、pt、mm)
- 关于加快INSERT语句执行速度和HINT /*+ append */及/*+ append nologging */的使用
- android性能优化--overdraw优化
- Android 查看內存使用
- 第6篇,企业如何创建微信公众平台?
- 移动端事件对象touches的误区
- iOS学习之NSArray的排序方法
- 基于融云SDK实现高仿微信