我实际上在iOS应用程序中使用了两个WebView。我想在启动应用程序时在我的SecondViewController中预加载第二个WebView。我曾尝试使用NSNotification Center预加载WebView,但它不起作用。如何在SecondViewController中预加载WebView?
答案 0 :(得分:0)
我通过以下方式解决了类似的问题:
似乎有必要在视图控制器中以编程方式创建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)
}
}