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

时间:2019-05-18 20:00:12

标签: ios swift webview appdelegate

在appDelegate中,我有func,当app打开时会收到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
 }

1 个答案:

答案 0 :(得分:0)

创建一个包含WebView的类(UIViewController),并在服务器返回响应后调用它。....

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