Home > Enterprise >  Argument passed to call that takes no arguments on swift for Webview
Argument passed to call that takes no arguments on swift for Webview

Time:11-10

I am building a webview on xcode with swift and the error

Argument passed to call that takes no arguments on swift for Webview

I am building a webview on xcode with swift and the error Argument passed to call that takes no arguments on swift for Webview

//
//  ContentView.swift
//  GenuineApp
//
//  Created by Kennysoft-Macbook on 11/10/22.
//

import SwiftUI
import WebKit

struct ContentView: View {
    @State private var showWebView = false
    private let urlstring: String = "https://www.genuineict.com"
    var body: some View {
        VStack(spacing: 40) {
            WebView(url: URL(string: urlstring)!).frame(height: 500.0)
                .cornerRadius(10)
        }
    }
}
struct WebView: UIViewRepresentable {
    var url:URL
    
    func makeUIView(context: Context) -> some UIView {
        return WKWebView()
    }
    
    func updateUIView(_ uiView: UIViewType, context: Context) {
        let request = URLRequest(url: url)
        UIView.load(request) Here is the error Argument passed to call that takes no arguments on swift for Webview
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

This looks good but it is not working on the latest xcode is there anyone using xcode with swift in 2022 Am building a webview on xcode with swift and the error Argument passed to call that takes no arguments on swift for Webview

CodePudding user response:

The UIView.load() method you are calling doesn't take any arguments, so XCode is correct in their assessment. You can control-click on the .load method and go to the documentation to verify this.

What you probably wanted to do is pass the URLRequest to the WKWebView that you're creating. By making the return value of makeUIView specific (so not using some to make it opaque), the updateUIView method will also recognize it as the incoming uiView. You can then call the correct WKWebView.load(_:) on it:

struct WebView: UIViewRepresentable {
    var url: URL
    
    func makeUIView(context: Context) -> WKWebView {
        WKWebView()
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        let request = URLRequest(url: url)
        uiView.load(URLRequest(url: url))
    }
}

CodePudding user response:

Change your updateUIView to so that you call load on your web view instance instead

func updateUIView(_ webView: WKWebView, context: Context) {
    let request = URLRequest(url: url)
    webView.load(request)
}
  • Related