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: 4
Upvote 0

Similar Threads

Top