0

I am creating one SDK to get the url from user and load. So ai m creating my own class with WKWebview. But i am getting few issues about Instance member 'webView' cannot be used on type 'MyWebView(UIview)'

Code :

import Foundation
import WebKit

public class MyWebView: UIView, WKNavigationDelegate {

    // initialize the view
     var webView: WKWebView!
    
    // load the view
    private func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
    }
    
    // get the url and load the page
    public static func loadUrl(Url: String) {
        MyWebView.webView.load(URLRequest(url: URL(string: Url)!))
    }
    
    
    
}

In my loadUrl, what ever user sending i need to use that url and load the url. Same in my view controller will look like :

import UIKit

class ViewController: UIViewController {

    var webView: MyWebView!
    
    override func loadView() {
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

Any help would be great.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
kumar barian
  • 139
  • 2
  • 12

1 Answers1

2

Your loadUrl function should not be static, since it needs access to an instance property, webView. Making the function non-static solves the issue.

Also a couple of minor improvements: don't force unwrap the URL init, since with an incorrect input that will crash. Use optional binding to safely unwrap it instead. I'd also suggest renaming the input argument label on loadUrl, since there's no point in having to right out loadUrl(Url:) every time you call the func, loadUrl( reads more naturally.

public func loadUrl(_ urlString: String) {
    guard let url = URL(string: urlString) else { return }
    webView.load(URLRequest(url: url))
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • `public func passUrl(url: String) { guard let url = URL(string: url) else { return } webView.load(URLRequest(url: url)) webView.allowsBackForwardNavigationGestures = true webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil) }` – kumar barian Jul 29 '20 at 09:24
  • I can able to call in my view controller. But its getting crash like `webView.passUrl(url: "https://www.swiggy.com")`. Crash : `Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file , line 7` – kumar barian Jul 29 '20 at 09:31
  • Any help for here ? https://stackoverflow.com/questions/63150713/getting-black-screen-in-load-of-webview-url-from-my-own-class – kumar barian Jul 29 '20 at 09:45
  • 1
    @kumarbarian have a look at [Unexpectedly found nil while unwrapping an optional value](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu), it explains pretty much all possible causes of that crash (and their fixes as well) – Dávid Pásztor Jul 29 '20 at 09:45
  • Crash i have fixed. But i am getting some black screen on load the URL https://stackoverflow.com/questions/63150713/getting-black-screen-in-load-of-webview-url-from-my-own-class – kumar barian Jul 29 '20 at 09:47