【问题标题】:How to preload a UIWebView in another ViewController when launching the application in swift 3?在 swift 3 中启动应用程序时,如何在另一个 ViewController 中预加载 UIWebView?
【发布时间】:2017-11-02 19:23:03
【问题描述】:

我实际上在我的 iOS 应用程序中使用了两个 WebView。我想在启动应用程序时在我的 SecondViewController 中预加载第二个 WebView。我尝试使用 NSNotification Center 预加载 WebView,但它不起作用。如何在 SecondViewController 中预加载 WebView?

【问题讨论】:

    标签: ios swift3 webview uiwebview


    【解决方案1】:

    我通过以下方式解决了类似的问题:

    1. 预加载一个包含 webview 的视图控制器并将引用存储在 Appdelegate 中。
    2. 在 AppDelegate 中调用 self.preloadvc?.view.layoutSubviews()
    3. 您可以随时展示它。

    似乎有必要在视图控制器中以编程方式创建 Web 视图

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    //
    var preloadvc : PreloadVC? = nil
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
                //Preloading
            DispatchQueue.main.async(execute: {
                        self.preloadvc = PreloadVC()
                        //Thats the trick for preloading the view
                        self.preloadvc?.view.layoutSubviews()
                    })
        return true
    }
    

    }

    class PreloadVC : UIViewController, UIWebViewDelegate {
    var mypreloadedView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        mypreloadedView = UIWebView(frame: view.frame)
        view.addSubview(mypreloadedView)
        mypreloadedView.delegate = self
        if let myurl = URL(string: "https://www.google.de") {
            let request = URLRequest(url: myurl)
            self.mypreloadedView.loadRequest(request)
        }
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        print("Loaded")
    }
    

    }

    随时随地展示:

    @IBAction func ShowPreloaded(_ sender: Any) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        if let preloadedvc = appDelegate.preloadvc {
            self.present(preloadedvc, animated: true, completion: nil)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多