2013-01-24 130 views
0

我用這個也:如何處理警報點擊從UIWebview產生的警報。

@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]; 
} 
+0

能否請您對一個UIWebView –

+0

創建類別共享代碼在這個類別看看https://github.com/wangruofeng/IOS-Categories/blob /master/IOS-Categories/UIKit/UIWebView/UIWebView%2BAlert.m –

回答

1

你必須使用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 
     } 
    } 
} 
+0

嘿不工作 – vipulkumarmehta

0

可以顯示在Javascript警報 - 這看起來完全像一個UIAlertView。然後關閉警報,將用戶重定向到Javascript中的「someaction://」。在設備上,實現UIWebViewDelegatewebView:shouldStartLoadWithRequest:navigationType:像這樣:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 

    if ([request.URL absolutePath] hasPrefix:@"someaction://"]) { 
     // do what you want to do after the alert has been dismissed 
    } 

    return NO; 
}