码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 定制浏览器(使用UIWebView)

时间:2014-05-14 21:39:51      阅读:457      评论:0      收藏:0      [点我收藏+]

标签:uiwebview


iOS 定制浏览器(使用UIWebView)

 

 

UIWebView 本身自带了前进,后退,刷新,停止等方法。

所以我们只需要调用现有的借口就可以完成一款应用内嵌的浏览器了。

比方说系统提供了如下的方法:

- (void)reload;

- (void)stopLoading;


- (void)goBack;

- (void)goForward;


并且提供了一下的几个属性来标示这几个方法是否可用:

@property(nonatomic,readonly,getter=canGoBack) BOOL canGoBack;

@property(nonatomic,readonly,getter=canGoForward) BOOL canGoForward;

@property(nonatomic,readonly,getter=isLoading) BOOL loading;

 

加载请求需要调用如下几个方法,比如说(加载html文件,加载NSData数据,加载网络请求)

 

- (void)loadRequest:(NSURLRequest *)request;

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;

- (void)loadData:(NSData *)data 

MIMEType:(NSString *)MIMEType 

textEncodingName:(NSString *)textEncodingName 

 baseURL:(NSURL *)baseURL;

 

 

加载网络请求需要一个网络地址(NSUrl*)

eg:

        NSURL* url = [NSURL URLWithString:@"http://www.google.com.hk"];
        NSURLRequest*request = [NSURLRequest requestWithURL:_currenURL];
        [webView_ loadRequest:request];


新建一个工程,在首页上添加一个UIButton 和一个UITextField 分别用来执行事件和输入网址。

然后新建一个BrowserController控制器,用来加载UIWebView

然后再BrowserController控制器中定义如下几个变量:


    //浏览器

    UIWebView                   *webView;

    //功能栏

    UIView                      *toolBar;

    //功能按钮

    UIButton                    *stopButton;//停止加载

    UIButton                    *previousButton;//后退

    UIButton                    *nextButton;//前进

    UIButton                    *reloadButton;//刷新

    //当前的url

    NSURL *_currenURL;


这些变量将用来加载数据和执行前进后退和刷新等得操作。


我们还需要根据不同屏幕大小来创建不同大小的WebView,所以定义如下几个宏定义:

//屏幕宽度

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

//屏幕高度

#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

//导航栏高度

#define NAVIGATION_BAR_HEIGHT 44.0f

//状态栏高度

#define STATUS_BAR_HEIGHT 20.0f

//工具栏高度

#define TOOL_BAR_HEIGHT 30


此外还需要定义如下几个方法:


//加载请求

- (void)loadUrl:(NSString *)url;

- (void)loadURLof:(NSURL *)url;

//初始化功能栏

- (void)loadToolBar;

//刷新功能栏按钮

- (void)reflashButtonState;

//创建等待视图

- (void)createLoadingView;

//刷新等待视图

- (void)freshLoadingView:(BOOL)b;


应为不使用xib,所以重写-(void)loadView;方法来创建界面。


-(void)loadView{
    [super loadView];

    if (webView == nil)
    {
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT - 30)];
        webView.delegate = self;
        webView.scalesPageToFit = YES;
        [self.view addSubview:webView];
    }

    if(!toolBar)
    {
        [self loadToolBar];
    }
}



/**
 * Description: 加载功能栏
 * Input:
 * Output:
 * Return:
 * Others:
 */
- (void)loadToolBar
{
    float toolY = self.view.frame.size.height - 30-44-20;
    toolBar = [[UIView alloc] initWithFrame:CGRectMake(0.0, toolY, 320.0, 30.0)];
    toolBar.backgroundColor = [UIColor orangeColor];

    //webView images
    UIImage *stopImg = [UIImage imageNamed:@"stopButton.png"];
    UIImage *nextImg = [UIImage imageNamed:@"nextButtonWeb.png"];
    UIImage *previousdImg =[UIImage imageNamed:@"previousButton.png"];
    UIImage *reloadImg =[UIImage imageNamed:@"reloadButton.png"];

    //功能按钮
    stopButton = [[UIButton alloc]initWithFrame:CGRectMake(44.0, 3.0, 24.0, 24.0)];
    [stopButton setImage:stopImg forState:UIControlStateNormal];
    [stopButton addTarget:self action:@selector(stopWebView:) forControlEvents:UIControlEventTouchUpInside];

    previousButton = [[UIButton alloc]initWithFrame:CGRectMake(112.0, 3.0, 24.0, 24.0)];
    [previousButton setImage:previousdImg forState:UIControlStateNormal];
    [previousButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];

    nextButton = [[UIButton alloc]initWithFrame:CGRectMake(180.0, 3.0, 24.0, 24.0)];
    [nextButton setImage:nextImg forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(forward:) forControlEvents:UIControlEventTouchUpInside];

    reloadButton = [[UIButton alloc]initWithFrame:CGRectMake(248.0, 3.0, 24.0, 24.0)];
    [reloadButton setImage:reloadImg forState:UIControlStateNormal];
    [reloadButton addTarget:self action:@selector(reload:) forControlEvents:UIControlEventTouchUpInside];

    [toolBar addSubview:stopButton];
    [toolBar addSubview:previousButton];
    [toolBar addSubview:nextButton];
    [toolBar addSubview:reloadButton];

    [self.view addSubview:toolBar];

}

其他的方法:

#pragma mark - webView actions

- (void)back:(id)sender
{
    if (webView.canGoBack)
    {
        [webView goBack];
    }
}

- (void)reload:(id)sender
{
    [webView reload];
    [self freshLoadingView:YES];
}

- (void)forward:(id)sender
{
    if (webView.canGoForward)
    {
        [webView goForward];
    }
}

- (void)stopWebView:(id)sender
{
	[webView stopLoading];
}

- (void)loadUrl:(NSString *)url
{
    if (webView)
    {
        url = [url stringByReplacingOccurrencesOfString:@"%26" withString:@"&"];

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];

        [webView loadRequest:request];
    }
}

- (void)loadURLof:(NSURL *)url
{
    self.currenURL = url;
}

- (void)reflashButtonState
{
    if (webView.canGoBack)
    {
        previousButton.enabled = YES;
    }
    else
    {
        previousButton.enabled = NO;
    }

    if (webView.canGoForward)
    {
        nextButton.enabled = YES;
    }
    else
    {
        nextButton.enabled = NO;
    }
}

创建等待视图:


-(void)createLoadingView
{

    UIView* v = [UIApplication sharedApplication].keyWindow;
    hub = [[MBProgressHUD alloc] initWithView:v];
    hub.delegate = self;
    hub.removeFromSuperViewOnHide = YES;
	hub.labelText = @"加载中...";
    [v addSubview:hub];
    //这个地方得注意一下,设置hub 的frame的view 必须和 要添加hub 的view 一致,不然他妈的崩溃的一塌糊涂。
}

-(void)freshLoadingView:(BOOL)b
{
    if (b) {
        [hub show:YES];
        [hub hide:YES afterDelay:3];
    }
    else{
        [hub hide:YES];
    }
}




UIWebViewDelegate 代理方法中处理功能栏的刷新,和等待视图。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    [self reflashButtonState];
    [self freshLoadingView:YES];

    NSURL *theUrl = [request URL];
    self.currenURL = theUrl;
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self reflashButtonState];
    [self freshLoadingView:NO];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self reflashButtonState];
    [self freshLoadingView:NO];
}


IOS 定制浏览器(使用UIWebView),布布扣,bubuko.com

IOS 定制浏览器(使用UIWebView)

标签:uiwebview

原文地址:http://blog.csdn.net/shouqiangwei/article/details/25803259

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!