iOS Question Webview JS

tariqyounis

Member
Licensed User
Longtime User
Dear all;

I have a B4i webview where a user has to press cancel to return back to the previous page. The problem is that when the user presses the cancel an alert will popup saying are you sure to answer yes or no. ON B4a the alert is shown, but in B4i the alert is not shown, so the user cannot do any thing and he is stuck on that page. He has to close the app.

Any advice
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example of handling JS confirm dialog:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private WebView1 As WebView
    Private WebViewDelegate As NativeObject
End Sub

Public Sub Initialize
    
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebViewDelegate = WebViewDelegate.Initialize("MyUIDelegate").RunMethod("new", Null)
    WebView1.As(NativeObject).SetField("UIDelegate", WebViewDelegate)
    WebView1.LoadUrl("https://www.b4x.com/maps.html")
    
End Sub

Private Sub WebView1_Confirm (Message As String, url As String)
    Msgbox2("Msg", url & " asking: " & CRLF & Message, "", Array ("Yes", "Cancel"))
    Wait For Msg_Click (ButtonText As String)
    WebViewDelegate.RunMethod("confirm:", Array(ButtonText = "Yes"))
End Sub

#if OBJC
@end
@interface MyUIDelegate :NSObject<WKUIDelegate>
@property (nonatomic, copy) void (^handler)(BOOL result);
@end
@implementation MyUIDelegate {
    
}
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
   NSLog(@"test runJavaScriptAlertPanelWithMessage");
   completionHandler();
}

- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler {
   NSLog(@"test runJavaScriptConfirmPanelWithMessage");
   self.handler = completionHandler;
   [B4IObjectWrapper raiseUIEvent:webView :@"_confirm::" :@[message, webView.URL.absoluteString]];
}
- (void) confirm:(BOOL)result {
    self.handler(result);
    self.handler = nil;
}

#End If
 

Attachments

  • Project.zip
    8.5 KB · Views: 57
Upvote 0

tariqyounis

Member
Licensed User
Longtime User
Example of handling JS confirm dialog:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private WebView1 As WebView
    Private WebViewDelegate As NativeObject
End Sub

Public Sub Initialize
   
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebViewDelegate = WebViewDelegate.Initialize("MyUIDelegate").RunMethod("new", Null)
    WebView1.As(NativeObject).SetField("UIDelegate", WebViewDelegate)
    WebView1.LoadUrl("https://www.b4x.com/maps.html")
   
End Sub

Private Sub WebView1_Confirm (Message As String, url As String)
    Msgbox2("Msg", url & " asking: " & CRLF & Message, "", Array ("Yes", "Cancel"))
    Wait For Msg_Click (ButtonText As String)
    WebViewDelegate.RunMethod("confirm:", Array(ButtonText = "Yes"))
End Sub

#if OBJC
@end
@interface MyUIDelegate :NSObject<WKUIDelegate>
@property (nonatomic, copy) void (^handler)(BOOL result);
@end
@implementation MyUIDelegate {
   
}
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
   NSLog(@"test runJavaScriptAlertPanelWithMessage");
   completionHandler();
}

- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler {
   NSLog(@"test runJavaScriptConfirmPanelWithMessage");
   self.handler = completionHandler;
   [B4IObjectWrapper raiseUIEvent:webView :@"_confirm::" :@[message, webView.URL.absoluteString]];
}
- (void) confirm:(BOOL)result {
    self.handler(result);
    self.handler = nil;
}

#End If
Thank you for your reply, but I am still not able to display the alert that is coming from the html page.
the link to the page: so in b4i webview if a person presses "Click me to show alert" an alert should be displayed saying "Good Job"
 
Upvote 0

tariqyounis

Member
Licensed User
Longtime User
This is not a confirmation dialog. The user can only click "ok".

You should see this message in the logs: NSLog(@"test runJavaScriptAlertPanelWithMessage");
You are absolutely right, but how to take an action like moving the webview.loadurl based on the message I get.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change the OBJC method to:
B4X:
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
   NSLog(@"test runJavaScriptAlertPanelWithMessage");
   [B4IObjectWrapper raiseUIEvent:webView :@"_alert::" :@[message, webView.URL.absoluteString]];
   completionHandler();
}

Catch this event with:
B4X:
Private Sub WebView1_Alert (Message As String, url As String)

You don't need to call the "confirm:" method.
 
Upvote 0
Top