如何处理警报单击从UIWebview生成警报的位置。

时间:2013-01-24 19:03:20

标签: javascript iphone ios

我也用过这个:

@interface UIWebView (JavaScriptAlert)

 - (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;

@end

@implementation UIWebView (JavaScriptAlert)

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
    UIAlertView *dialogue = [[UIAlertView alloc] initWithTitle:@"My Alert Title" message:message delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    [dialogue show];
}

2 个答案:

答案 0 :(得分:0)

您必须使用UIAlertView的委托

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
    UIAlertView *dialogue = [[UIAlertView alloc] initWithTitle:@"My Alert Title" message:message delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    dialogue.tag = 1;
    [dialogue show];
}


-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1) {
         if (buttonIndex == alertView.cancelButtonIndex) {
             //Do something here
         }
    }
}

答案 1 :(得分:0)

您可以在Javascript中显示提醒 - 这看起来与UIAlertView完全相同。然后在解除警报时,将用户重定向到Javascript中的“someaction://”。在设备上,实现UIWebViewDelegate的{​​{1}},如下所示:

webView:shouldStartLoadWithRequest:navigationType: