標籤: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];}