0

我一直试图在 UIWebView 上收到“javascript 警报”消息。虽然可以通过“onJsAlert”事件在 Android WebView 上获得一个,但 UIWebView 没有一种方法......如果有人知道如何做到这一点,请告诉我。

4

1 回答 1

3

你不能以简单的方式(ASAIK)做到这一点。让我们破解它!尝试重新定义你的alert函数,记得把你的js alert的alert消息传入

我是用于检测的特殊 URL

- (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;
}
于 2013-07-19T02:36:37.790 回答