iOS Question B4i 6.5 how to use WebView.InitializeWithCustomConfiguration

bdobaj

Member
Licensed User
Can anyone please give an example for WebView.InitializeWithCustomConfiguration ?? as from b4i 6.5 WKWebView is used as inernal WebWiew library

Is it possible to force this way the WebView will ignore Selfsigned certificate error or better to create handler for SSL errors and any other errors or events like PageFinished

on b4A this was possible with WebViewExtras2 library

Sub WebViewClient1_ReceivedSslError(SslErrorHandler1 As SslErrorHandler, SslError1 As SslError)
Sub WebViewClient1_PageFinished (Url As String)

Thanks in advance

Bojan from sLOVEnia
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example of making WebView skip the certificate validation:
B4X:
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

You should also disable the ATS option.
 
Upvote 0

bdobaj

Member
Licensed User
Thanks Erel,

It works :) WebView now skips certificate validation.

But WebView1_PageFinished is not thiggered

Can I cacth error codes from WebView - If yes - How?

Maybe as on B4a

Sub WebView1_ReceivedError(ErrorCode As Int, Description As String, FailingUrl As String)

End Sub



Another question.
I'm running IOS simulator on macbook, but WebView can acces only local pages and only few internet pages - not all. Is this limitation of Simulator or I'm doing something wrong?


Thanks in advance

Bojan
 
Last edited:
Upvote 0

bdobaj

Member
Licensed User
OK I understand.
Anyway question is related on your exaple posted on this thread? should this be in new thread?

Using your example:
WebView skips certificate validation :) but

WebView1_PageFinished (Success As Boolean, Url As String)

is newer triggered - what can be wrong?
 
Upvote 0
Top