I have been trying to use the WKWebView in my SwiftUI app. To do that, I created a wrapper that allows me to customize my WKWebView.
This view should serve a single purpose : I need to grab an authentication token from a callback.
That it why I need the last method you can find at the bottom of my CustomWebView class:
import Foundation
import SwiftUI
import WebKit
struct WebView : UIViewRepresentable {
let request: URLRequest
func makeUIView(context: Context) -> WKWebView {
return CustomWebView().webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.load(request)
}
}
class CustomWebView: UIViewController, WKNavigationDelegate {
// Create a custom WKWebView that supports Javascript
public let webView: WKWebView = {
let prefs = WKWebpagePreferences()
prefs.allowsContentJavaScript = true
let config = WKWebViewConfiguration()
config.defaultWebpagePreferences = prefs
let webView = WKWebView(frame: .zero,
configuration: config)
return webView
}()
override func viewDidLoad() {
super.viewDidLoad()
title = "Sign In"
}
// Grab the authentication token
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
guard let url = webView.url else {
return
}
// Exchange the code for the authentication access token
guard let code = URLComponents(string: url.absoluteString)?.queryItems?.first(where: { $0.name == "code" })?.value else {
return
}
print("Code: \(code)")
}
}
If I don't use this last method, my code runs without any error.
But as soon as I run it, the callback loads correctly, but I get multiple errors in the console:
2022-06-21 16:50:17.985187+0200 MyAppsName[39758:7506939] [assertion] Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}>
2022-06-21 16:50:17.985870+0200 MyAppsName[39758:7506939] [ProcessSuspension] 0x1180642a0 - ProcessAssertion: Failed to acquire RBS assertion 'ConnectionTerminationWatchdog' for process with PID=39759, error: Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}
I tried to look for specific lines in "Signing & Capabilities" that could unlock any WebKit capabilities, but I can't find anything. Does anyone have a lead?