標籤:
①像普通controller那樣實現跳轉到webview的效果,而不是直接加到當前controller
②隱藏webview的某些元素
③webview跳往原生app
④給webview添加進度條
解決方案如下:
①使用webview的基本步驟
NSURL *url = [NSURL URLWithString:self.urlStr];NSURLRequest *request = [NSURLRequest requestWithURL:url];[self.webView loadRequest:request];-(UIWebView *)webView{ if (!_webView) { _webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds]; _webView.delegate = self; [self.view addSubview:_webView]; } return _webView;}
但這樣webview是直接加在當前controller的,要實現跳轉效果,應當寫個controller,裡邊再添加webview
跳轉是這樣的
MyWebViewController *web = [[MyWebViewController alloc]initWithUrl:urlGo];[self.navigationController pushViewController:web animated:YES];
MyWebViewController是這樣定義的
.h檔案
@interface MyWebViewController : UIViewController<UIWebViewDelegate>@property(nonatomic,copy) NSString *urlStr;@end
.m內
@interface MyWebViewController ()@property(nonatomic,strong) UIWebView *webView;@end@implementation MyWebViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request];}-(UIWebView *)webView{ if (!_webView) { _webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds]; _webView.delegate = self; [self.view addSubview:_webView]; } return _webView;}@end
這樣就能實現webview的跳轉和基本使用
②隱藏webview的元素
這需要在webview的代理方法中實現,並且用js來操作。先在chrome上用開發人員工具,擷取要隱藏的元素的class,然後用
document.documentElement.getElementsByClassName(‘元素的class‘)[0].style.display = ‘none‘
//網頁載入完成-(void)webViewDidFinishLoad:(UIWebView *)webView{ //去除某個按鈕 [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.getElementsByClassName(‘ibar-toggle‘)[0].style.display = ‘none‘"]; //將controller標題改為webview標題 NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"]; if (title.length) { self.title = title; }}
③webview跳往原生app
這同樣需要在webview的代理方法中實現
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
在需要跳往webview的地方返回YES,跳往原生app的地方返回NO,再添加跳原生app的方法。這需要攔截url內的字串判斷比較。如跳往商品詳情頁
http://xxx.com/wap/getItemDetail/146762587051761769.htm
//網頁載入之前執行該方法-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ NSString *resuestStr = request.URL.absoluteString; if ([resuestStr rangeOfString:@"getItemDetail"].location != NSNotFound) { NSArray *tempArr1 = [resuestStr componentsSeparatedByString:@"getItemDetail/"]; NSString *tempStr1 = [tempArr1 lastObject]; NSString *itemCode = [tempStr1 stringByReplacingOccurrencesOfString:@".htm" withString:@""]; ProductDetailViewController *vc = [[ProductDetailViewController alloc]initWithItemCode:itemCode]; [self.navigationController pushViewController:vc animated:YES]; return NO; } return YES;}
④給webview添加進度條
這需要用到第三方庫NJKWebViewProgress,github地址為https://github.com/ninjinkun/NJKWebViewProgress
上邊寫的已經很清楚
要改變進度條的顏色,需要在NJKWebViewProgressView.m裡configureViews方法修改
UIColor *tintColor = [UIColor colorWithRed:0/255.0 green:224/255.0 blue:94/255.0 alpha:1];
UIWebView使用時的問題