How to attach the Safari Debugger to the simulator in a turbo-ios native app

69 Views Asked by At

I want to attach the Safari Debugger to the simulator in a turbo-ios native app. According to Joe Masilotti's blog post, you need to add the following code

    let webView = WKWebView()
if #available(iOS 16.4, *) {
    webView.isInspectable = true
}

let session = Session(webView: webView)

I cannot work out where to put this code. I have tried to do this in the class SceneDelegate as follows

private let webViewConfiguration: WKWebViewConfiguration = {
    let configuration = WKWebViewConfiguration()
    configuration.applicationNameForUserAgent = " IOS-xyz/1.0"
    // Enable inspection for Safari debugger
    if #available(iOS 16.4, *) {
        configuration.isInspectable = true
    }

but this gives an error

Value of type 'WKWebViewConfiguration' has no member 'isInspectable' 

So where do I put this code?

For context, here is the entire SceneDelegate Class, which is fairly standard for turbo-ios apart from the use of WKWebViewConfiguration.

import UIKit
import Turbo
import WebKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    private lazy var navigationController = UINavigationController()
    
    private let webViewConfiguration: WKWebViewConfiguration = {
        let configuration = WKWebViewConfiguration()
        configuration.applicationNameForUserAgent = " IOS-xyz/1.0"
        return configuration
    }()

    private func visit(url: URL) {
        let viewController = VisitableViewController(url: url)
        navigationController.pushViewController(viewController, animated: true)
        session.visit(viewController)
    }

    // Initialize the Turbo Session with the custom configuration
    private lazy var session: Session = {
        let session = Session(webViewConfiguration: webViewConfiguration)
        session.delegate = self
        return session
    }()

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
        window!.rootViewController = navigationController
        visit(url: URL(string: "http://myserver.com/sessions/new")!)
    }
}

extension SceneDelegate: SessionDelegate {
    func session(_ session: Session, didProposeVisit proposal: VisitProposal) {
        visit(url: proposal.url)
    }
    
    func session(_ session: Session, didFailRequestForVisitable visitable: Visitable, error: Error) {
        print("didFailRequestForVisitable: \(error)")
    }
    
    func sessionWebViewProcessDidTerminate(_ session: Session) {
        session.reload()
    }
}
1

There are 1 best solutions below

3
On BEST ANSWER

isInspectable is a property of WKWebView and it's not available within WKWebViewConfiguration.

private let webViewConfiguration: WKWebViewConfiguration = {
    let configuration = WKWebViewConfiguration()
    configuration.applicationNameForUserAgent = " IOS-xyz/1.0"
    // other configuration
    return configuration
}()

private lazy var session: Session = {
    let webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
    if #available(iOS 16.4, *) {
        webView.isInspectable = true
    }
    let session = Session(webView: webView)
    session.delegate = self
    return session
}()

enter image description here