Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private WebView1 As WebView
Private WebView1Delegate As NativeObject
End Sub
Public Sub Initialize
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
WebView1Delegate = WebView1Delegate.Initialize("B4IWebViewDelegate2").RunMethod("new", Null)
Dim no As NativeObject = WebView1
no.SetField("navigationDelegate", WebView1Delegate)
End Sub
Sub WebView1_PageFinished (Success As Boolean, Url As String)
If Success = False Then
Log(LastException)
End If
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Private Sub Button1_Click
xui.MsgboxAsync("Hello world!", "B4X")
End Sub
#if OBJC
@end
#import <WebKit/WebKit.h>
@interface B4IWebViewDelegate2:NSObject <WKNavigationDelegate, WKUIDelegate>
@end
@implementation B4IWebViewDelegate2
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[B4IObjectWrapper raiseUIEvent:webView :@"_pagefinished::" :@[@(true), webView.URL.absoluteString]];
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[self webView:webView didFailProvisionalNavigation:navigation withError:error];
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
[[B4I shared] setLastError:error];
NSString* url = webView.URL.absoluteString;
if (url == nil)
url = error.userInfo[NSURLErrorFailingURLStringErrorKey];
if (url == nil)
url = @"";
[B4IObjectWrapper raiseUIEvent:webView :@"_pagefinished::" :@[@(false), url]];
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL *url = navigationAction.request.URL;
BOOL b;
if ([url isFileURL] && [url isEqual:[NSURL fileURLWithPath:[B4ICommon shared].File.DirAssets]])
b = true;
else {
NSObject *res = [B4IObjectWrapper raiseEvent:webView :@"_overrideurl:" :@[url.absoluteString]];
b = res == nil ? true : ![(NSNumber *) res boolValue];
}
decisionHandler(b ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel);
}
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
CFDataRef exceptions = SecTrustCopyExceptions (serverTrust);
SecTrustSetExceptions (serverTrust, exceptions);
CFRelease (exceptions);
completionHandler (NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:serverTrust]);
}
#End If