//
// myWebViewController.m
// webView
//
// Created by wusiping on 15/11/22.
// Copyright (c) 2015年 wusiping. All rights reserved.
//
#import "myWebViewController.h"
@interface myWebViewController ()
@property(nonatomic, weak) UIWebView *myWeb;
@property(nonatomic, weak) UIToolbar *myToolBar;
@property(nonatomic, copy) NSString *currentUrl;
@end
@implementation myWebViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setWebView];
[self addReturnTool];
[self request];
}
- (void)request
{
//定好url
NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];
//创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//发送请求给服务器
[self.myWeb loadRequest:request];
}
- (void)addReturnTool
{
UIToolbar *toolBar = [[UIToolbar alloc]init];
CGFloat toolBarX = 0;
CGFloat toolBarY = 20;
CGFloat toolBarW = [UIScreen mainScreen].bounds.size.width;
CGFloat toolBarH = 50;
CGRect toolBarFrame = CGRectMake(toolBarX, toolBarY, toolBarW, toolBarH);
toolBar.frame = toolBarFrame;
[toolBar setBackgroundColor:[UIColor whiteColor]];
//给toolbar添加按钮
UIButton *leftButton = [[UIButton alloc]init];
CGFloat leftButtonX = 25;
CGFloat leftButtonY = 5;
CGFloat leftButtonW = 50;
CGFloat leftButtonH = 30;
CGRect leftButtonFrame = CGRectMake(leftButtonX, leftButtonY, leftButtonW, leftButtonH);
leftButton.frame = leftButtonFrame;
[leftButton setTitle:@"返回" forState:UIControlStateNormal];
[leftButton setBackgroundColor:[UIColor grayColor]];
//leftButton添加点击操作
[leftButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[toolBar addSubview:leftButton];
_myToolBar = toolBar;
[self.view addSubview:_myToolBar];
}
- (void)goBack
{
// 判断是否可以返回,如果可以,就返回
if (_myWeb.canGoBack) {
_currentUrl = _myWeb.request.URL.absoluteString;
NSLog(@"%@",_currentUrl);
//判断当前url 是否== 某个连接,如果等于就执行某个操作(比如直接返回主页)
if ([_myWeb.request.URL.absoluteString isEqual: @"http://m.baidu.com"]) {
//返回主页相关代码
}
[_myWeb goBack];
}
}
- (void)setWebView
{
UIWebView * myWeb = [[UIWebView alloc]init];
CGFloat webX = 0;
CGFloat webY = 70;
CGFloat webW = [UIScreen mainScreen].bounds.size.width;
CGFloat webH = [UIScreen mainScreen].bounds.size.height - 49;
CGRect webFrame = CGRectMake(webX, webY, webW, webH);
myWeb.frame = webFrame;
_myWeb = myWeb;
[self.view addSubview:_myWeb];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end