"_ setUrl: unrecognized selector sent to instance" setting the url property to the Swift Publisher

103 views Asked by At

I want to set the url variable that Published in Swift

Error:

2022-12-15 21:55:27.837982+0100 WebviewIosExample[23481:8227336] [native] Error setting property 'url' of WebView with tag #7: Exception thrown while executing UI block: 
-[_TtGC7SwiftUI14_UIHostingViewV24react_native_webview_ios7WebView_ setUrl:]: unrecognized selector sent to instance 0x104417700
//WebViewManager.swift
@available(iOS 14.0, *)
@objc(WebViewManager)
class WebViewManager: RCTViewManager {
    
    public var vc = UIHostingController(rootView: WebView())
    
    @objc var url: String {
        set { vc.rootView.props.url = newValue }
        get { return vc.rootView.props.url }
    }
    
    @objc override func view() -> UIView! {
        return vc.view
    }
    
    override static func requiresMainQueueSetup() -> Bool {
        return true
    }
    
}
//WebViewManager.m

#import <React/RCTViewManager.h>

@interface RCT_EXTERN_MODULE(WebViewManager, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(url, NSString)

@end

// Bridgin-Header
#import <React/RCTViewManager.h>

// WebView.Swift
@available(iOS 14.0, *)
struct WebView: UIViewRepresentable {
    
    @ObservedObject var props = WebViewStore()
    
    func makeUIView(context: Context) -> WKWebView {
        let prefs = WKWebpagePreferences()
        prefs.allowsContentJavaScript = true
        let config = WKWebViewConfiguration()
        config.defaultWebpagePreferences = prefs
        let webView = WKWebView(frame: .zero, configuration: config)
        return webView
    }
    
    func updateUIView(_ webView: WKWebView, context: Context) {
        guard let theUrl = URL(string: props.url) else {
            return
        }
        
        let request = URLRequest(url: theUrl)
        webView.load(request)
    }
    
}

I want to set the WebViewStoreManager url while it passed to from React Native Code

@objc var url: String {
    set { vc.rootView.props.url = newValue }
    get { return vc.rootView.props.url }
}

Usage in RN:

import { WebView } from 'react-native-webview-ios';

// ...

<WebView url="https://open.spotify.com" />;

I tried:

@objc(setUrl:)
public func setUrl(url: String) {
    vc.rootView.props.url = url
}
0

There are 0 answers