0

I want to use my variable 'real url' to url request. I declared real url in app delegate like this:

AppDeleGate.swift:

import ...

..
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var realurl : URL?
..

And, I want to use that url in DetailController

DetailController.swift:

...
@IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let ad = UIApplication.shared.delegate as? AppDelegate
        let request = URLRequest(url: URL(ad?.realurl)!)
        webView.configuration.preferences.javaScriptEnabled = true
        webView.load(request)
...

but error... in ad?.realurl enter image description here

how can I solve this problem?

2 Answers 2

2

realurl is already an optional url, why are you converting URL(ad?.realurl)!)?

    let ad = UIApplication.shared.delegate as? AppDelegate
    if  let url = ad?.realurl  {
        let request = URLRequest(url: url)
        webView.configuration.preferences.javaScriptEnabled = true
        webView.load(request)
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can try

let ad = UIApplication.shared.delegate as! AppDelegate
let request = URLRequest(url:ad.realurl!)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.