当appdelegate中的func触发时,如何打开webview?

 收藏

在appDelegate中,我有func,可在打开应用程序时接收Boolen。 如果Boolen == True,则应用必须打开webView。 我如何引用初始化webView的viewcontroller? 如何创建webview,在应用程序收到服务器的答复后会看到?

谢谢。

//这是appDelegate文件

 func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let handled = ApplicationDelegate.shared.application(app, open: url, options: options)

       struct jsonR: Codable{ 
          let ads_enabled: Bool
        }
        print(url)


    Alamofire.request("https://appgootop.com))").responseJSON { response in

        if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
            let product: jsonR = try! JSONDecoder().decode(jsonR.self, from: data)
            //print("Data: \(utf8Text)")
            print(product.ads_enabled)

            if product.ads_enabled == false{
                let url = URL(string: "google.com")!


                 }

            }
    }

    return handled
 }
回复
  • gest 回复

    Create a class (UIViewController) that contains WebView and call it after your server return response.....

    class WebViewContainer : UIViewController {
          open var link: String {
                  didSet{ webView.request(link)}
          }
    
          let webView: UIWebView = {
              let webView =// initialize your webView here
          }()
    
    
          override fun viewDidLoad() {
                  super.viewDidLoad()
    
                  view.addSubview(webView)
                   // add constraints here
          }
      }
    

    并在服务器响应如下后使用它:

    let webViewVC = WebViewContainer()
    window = UIWindow(frame: UIScreen.main.bounds)
    window.makeKeyAndVisible()
    window.rootViewController = webViewVC
    webViewVC.link = // assign your link here