我正在开发由Objective-C编码的iOS混合应用程序。我想在UIWebView中捕获"window.open"事件。我找到了这个代码。
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked ){
NSLog(@"opening link");
return NO;
}
....
...
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString * test = [webView stringByEvaluatingJavaScriptFromString:@"window.open = function (open) { return function (url, name, features) { window.location.href = url; return window; }; } (window.open);"];
//
NSLog(@"test:%@", test); << always print blank
....
...
..
}
info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
但它不仅捕获"window.open",还捕获其他链接事件。一些答案建议使用"stringByEvaluatingJavaScriptFromString",但我不明白。
你能解释一下,让初学者容易理解吗?我只想在UIWebView中捕获"window.open"事件。
您需要使用以下代码来检测窗口打开:
[webView stringByEvaluatingJavaScriptFromString:@"window.open = function (open) { return function (url, name, features) { window.location.href = url; return window; }; } (window.open);"];
尝试将上面的代码放在委托方法下面:
- (void)webViewDidFinishLoad:(UIWebView *)webView {}