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:@"https://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]; }