我一直试图在UIWebView上收到“javascript alert”的消息。 虽然可以通过“onJsAlert”事件使用Android WebView上的那个, UIWebView没有一种方法...... 如果有人知道如何做到这一点,请告诉我。
答案 0 :(得分:3)
你不能以简单的方式做到这一点(ASAIK)。 我们破解吧! 尝试重新定义警报功能,记得在
中传递js警报的警报消息我是检测
的特殊网址- (void) webViewDidFinishLoad: (UIWebView *) webView
{
[webView stringByEvaluatingJavaScriptFromString:@"window.alert = function(message) { window.location = \"I AM A SPECIAL URL FOR detecting\"; }"];
}
在警报网址中获取您想要的内容:
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* url = request.URL;
// look for our custom action to come through:
if ( [url.host isEqualToString: @"I AM A SPECIAL URL FOR detecting"] )
{
// parsing the url and get the message, assume there is a '='for value, if you are passing multiple value, you might need to do more
NSString* message = [[[[url query] componentsSeparatedByString: @"="] lastObject] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// show our alert
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle: @"My Custom Title"
message: message
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alertView show];
return NO;
}
return YES;
}