iOS Question WKWebView show Javascript alert()

nobbi59

Active Member
Licensed User
Longtime User
Im trying to show an alert(); message in WKWebView.

On stackoverflow i found this answer saying that the WebView is missing a "Delegate".

But I don't know how to implement the solution with B4i. Maybe someone has already fixed this issue with B4i or knows enough about ObjC to fix it.
 

JanPRO

Well-Known Member
Licensed User
Longtime User
Hi,
B4i only sets the navigation delegate of the Webview, so it's safe to set the UIDelegate and use the ui delegate methods with inline Objc code:
B4X:
Sub SetUIDelegate (W As WKWebView)
    Dim NaObj As NativeObject = W
    NaObj.SetField("UIDelegate",Me)
End Sub

#If Objc
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK"
                                                        style:UIAlertActionStyleCancel
                                                      handler:^(UIAlertAction *action) {
                                                          completionHandler();
                                                      }]];
                                                      
    [__page1.object presentViewController:alertController animated:YES completion:^{}];
}
#End If

Jan
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Do you have a B4xpages version of your code?
 
Upvote 0
Top