1.UIWebView
UIWebView不僅能加載網(wǎng)絡(luò)資源還可以加載本地資源,目前支持的常用的文檔格式如:html、pdf碰辅、docx、txt等介时。
UIWebView整個(gè)使用相當(dāng)簡(jiǎn)單:創(chuàng)建URL->創(chuàng)建請(qǐng)求->加載請(qǐng)求没宾,無(wú)論是加載本地文件還是Web內(nèi)容都是這三個(gè)步驟。UIWebView內(nèi)容加載事件同樣是通過(guò)代理通知外界沸柔,常用的代理方法如開始加載循衰、加載完成、加載出錯(cuò)等勉失,這些方法通掣迹可以幫助開發(fā)者更好的控制請(qǐng)求加載過(guò)程。
加載資源:
- (void)loadRequest:(NSURLRequest *)request;
常用的屬性和方法:
//重新加載(刷新)
- (void)reload;
//停?止加載
- (void)stopLoading;
//回退
- (void)goBack;
//前進(jìn)
- (void)goForward;
//需要進(jìn)?檢測(cè)的數(shù)據(jù)類型
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes
//是否能回退
@property(nonatomic,readonly,getter=canGoBack) BOOL canGoBack;
//是否能前進(jìn)
@property(nonatomic,readonly,getter=canGoForward) BOOL canGoForward;
//是否正在加載中
@property(nonatomic,readonly,getter=isLoading) BOOL loading;
//是否伸縮內(nèi)容至適應(yīng)屏幕當(dāng)前尺寸
@property(nonatomic) BOOL scalesPageToFit;
遵守UIWebViewDelegate協(xié)議乱凿,監(jiān)聽UIWebView的加載過(guò)程:
//開始發(fā)送請(qǐng)求(加載數(shù)據(jù))時(shí)調(diào)用:
- (void)webViewDidStartLoad:(UIWebView *)webView;
//請(qǐng)求完畢(加載數(shù)據(jù)完畢)時(shí)調(diào)?:
- (void)webViewDidFinishLoad:(UIWebView *)webView;
//請(qǐng)求錯(cuò)誤時(shí)調(diào)用:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
//監(jiān)聽UIWebView的加載過(guò)程:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
下面是一個(gè)例子:
在storyBoard中拖入如下控件:
searchBar的代理方法:
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self request:_searchBar.text];
[searchBar resignFirstResponder];
}
加載searchBar中的請(qǐng)求:
-(void)request:(NSString *)urlStr{
//創(chuàng)建url
NSURL *url;
//如果file://開頭的字符串則加載bundle中的文件
if([urlStr hasPrefix:kFILEPROTOCOL]){
//取得文件名
NSRange range= [urlStr rangeOfString:kFILEPROTOCOL];
NSString *fileName=[urlStr substringFromIndex:range.length];
url=[[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
}else if(urlStr.length>0){
//如果是http請(qǐng)求則直接打開網(wǎng)站
if ([urlStr hasPrefix:@"http"]) {
url=[NSURL URLWithString:urlStr];
}else{//如果不符合任何協(xié)議則進(jìn)行搜索
urlStr=[NSString stringWithFormat:@"http://m.bing.com/search?q=%@",urlStr];
}
urlStr=[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//url編碼
url=[NSURL URLWithString:urlStr];
}
//創(chuàng)建請(qǐng)求
NSURLRequest *request=[NSURLRequest requestWithURL:url];
//加載請(qǐng)求頁(yè)面
[_webView loadRequest:request];
}
WebView的代理方法:
-(void)webViewDidStartLoad:(UIWebView *)webView{
//顯示網(wǎng)絡(luò)請(qǐng)求加載
[UIApplication sharedApplication].networkActivityIndicatorVisible=true;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
//隱藏網(wǎng)絡(luò)請(qǐng)求加載圖標(biāo)
[UIApplication sharedApplication].networkActivityIndicatorVisible=false;
//設(shè)置按鈕狀態(tài)
[self setBarButtonStatus];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"error detail:%@",error.localizedDescription);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"系統(tǒng)提示" message:@"網(wǎng)絡(luò)連接發(fā)生錯(cuò)誤!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[alert show];
}
設(shè)置前進(jìn)后退按鈕:
-(void)setBarButtonStatus{
if (_webView.canGoBack) {
_barButtonBack.enabled=YES;
}else{
_barButtonBack.enabled=NO;
}
if(_webView.canGoForward){
_barButtonForward.enabled=YES;
}else{
_barButtonForward.enabled=NO;
}
}
運(yùn)行結(jié)果如下:
你可以在這里下載到代碼。
- (void)getAccessToken:(NSString *)requestToken
{
[HttpTool postWithPath:@"oauth2/access_token" params:@{
@"client_id" : kAppKey,
@"client_secret" : kAppSecret,
@"grant_type" : @"authorization_code",
@"redirect_uri" : kRedirectURI,
@"code" : requestToken
} success:^(id JSON) {
// 保存賬號(hào)信息
Account *account = [[Account alloc] init];
account.accessToken = JSON[@"access_token"];
account.uid = JSON[@"uid"];
[[AccountTool sharedAccountTool] saveAccount:account];
// 回到主頁(yè)面
ViewController *main = [[ViewController alloc]init];
if (main) {
[self presentViewController:main animated:YES completion:nil];
}
} failure:^(NSError *error) {
}];
}