UIWebView本身不支持彈出窗口衷戈,然而有需求要用,不僅需要彈出窗口骗污,而且需要在用戶關(guān)閉窗口時觸發(fā)一個javascript函數(shù)夜矗。研究了一下,沒有使用任何第三方庫實現(xiàn)了酣难。
基本的思路是在UIWebView中攔截window.open
和window.close
的調(diào)用谍夭。這里有2個技術(shù)問題:
1.如何在objective-c中改變javascript的行為?
我的思路是在頁面加載完之后注入javascript憨募。簡單的說就是webViewDidFinishLoad
中使用UIWebView 的stringByEvaluatingJavaScriptFromString
注入javascript代碼紧索。
2.如何用javasript調(diào)用objective-c代碼?
我使用URLScheme的方式來做馋嗜。簡單的來說齐板,就是讓定義2個URLScheme:
open://
:用來出發(fā)彈出窗口;
back://
:用來關(guān)閉窗口葛菇;后面可以跟一個參數(shù)甘磨,觸發(fā)window.close之后需要回調(diào)的javascript函數(shù);
然后在- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
中攔截URL請求就可以調(diào)用objective-c代碼了。
具體的相關(guān)代碼如下:
1.ViewController實現(xiàn)UIWebViewDelegate
眯停,在ViewDidLoad中將UIWebView的delegate設(shè)為自己:
_browser.delegate = self;
2.webViewDidFinishLoad中注入javascript
-(void) webViewDidFinishLoad:(UIWebView*)webView {
// this is a pop-up window
if (wvPopUp)
{
NSString *js = @"window.close = function () {window.location.assign(\"back://\" + window.location);};";
__unused NSString *jsOverrides = [webView
stringByEvaluatingJavaScriptFromString:js];
}else {
///其他處理
NSString *js = @"window.open = function (url, d1, d2) { window.location = \"open://\" + url; }";
__unused NSString *jsOverrides = [webView
stringByEvaluatingJavaScriptFromString:js];
}
}
}
3.在shouldStartLoadWithRequest中攔截URL請求
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString* requestURL = request.URL.absoluteString;
if ([requestURL rangeOfString:@"back://"].location == 0)
{
[self closePopup];
return NO;
} else if ([requestURL hasPrefix: @"open://"]){
NSString *newUrl = [requestURL stringByReplacingOccurrencesOfString:@"open://" withString:@""];
NSURLRequest *newRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:newUrl]];
UIWebView *wv = [self popUpWebview];
[wv loadRequest:newRequest];
return NO;
}
return YES;
}
4.彈出窗口的objectie-c代碼
- (UIWebView *) popUpWebview
{
// Create a web view that fills the entire window, minus the toolbar height
CGRect rect = CGRectMake(_browser.frame.origin.x+20,_browser.frame.origin.y+20,
_browser.frame.size.width-40,_browser.frame.size.height-40);
vwPopUpContainer = [[UIView alloc] initWithFrame: rect];
vwPopUpContainer.backgroundColor = [UIColor grayColor];
[self.view addSubview:vwPopUpContainer];
CGRect rectBtn = CGRectMake(10, 10, 60, 20);
btnClosePopup = [[UIButton alloc] initWithFrame:rectBtn];
[btnClosePopup setTitle:@"Close" forState:UIControlStateNormal];
[btnClosePopup addTarget:self action:@selector(closePopup) forControlEvents:UIControlEventTouchUpInside];
[vwPopUpContainer addSubview:btnClosePopup];
CGRect rectWeb = CGRectMake(10, 40, rect.size.width - 20, rect.size.height - 50);
UIWebView *webView = [[UIWebView alloc]
initWithFrame: rectWeb];
webView.scalesPageToFit = YES;
webView.delegate = self;
wvPopUp = webView;
[vwPopUpContainer addSubview:wvPopUp];
return webView;
}
5.關(guān)閉窗口處理包括回調(diào)觸發(fā)帶參數(shù)的javascript函數(shù),這里函數(shù)名為onClosePopo
- (void) closePopup {
if (wvPopUp) {
NSURL *url = wvPopUp.request.URL;
NSString *surl = url.absoluteString;
NSUInteger pos = [surl rangeOfString:@"?"].location;
NSString *paras = @"";
if (pos != NSNotFound && pos < [surl length]-1) {
paras = [surl substringFromIndex:pos + 1];
}
NSString *js = [NSString stringWithFormat: @"onClosePopo('%@');",paras ];
__unused NSString *jsOverrides = [_browser
stringByEvaluatingJavaScriptFromString:js];
[wvPopUp removeFromSuperview];
wvPopUp = nil;
}
if(btnClosePopup){
[btnClosePopup removeFromSuperview];
btnClosePopup = nil;
}
if(vwPopUpContainer){
[vwPopUpContainer removeFromSuperview];
vwPopUpContainer = nil;
}
}
2017年2月27日更新
完整的演示例子已經(jīng)上傳到 github