func loadWebKit() { let screenWidth:CGFloat = view.frame.size.width let screenHeight:CGFloat = view.frame.size.height // iPhone X , X以外は0となる if #available(iOS 11.0, *) { let window = UIApplication.shared.keyWindow topPadding = window!.safeAreaInsets.top } //self.wkWebView.frame = CGRect(x: 0, y:topPadding, width:screenWidth, height:screenHeight*0.6) self.wkWebView.frame = CGRect(x:-50, y:topPadding, width:screenWidth, height:screenHeight*0.6) //wkWebView.frame = view.frame wkWebView.navigationDelegate = self wkWebView.uiDelegate = self // スワイプでの「戻る・すすむ」を有効にする wkWebView.allowsBackForwardNavigationGestures = true //読み込むURL let str: String = "https://taiwan-travel.net" str = str.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)! if let url = URL(string: str) { let request = URLRequest(url: url) wkWebView.load(request) } view.addSubview(wkWebView) //念のためにviewの更新をする view.setNeedsLayout() //隠すこともできます//表示はfalse wkWebView.isHidden = true }
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { print("遷移開始") }
//起動時かどうかの判定 var _isLoaded: Bool! func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { print("読み込み完了") print(webView.title as Any) } else { //起動時スキップし、読み込み完了後に何かする print("\n*** first time ***") _isLoaded = true self.getResult() } self.getResult() print("webViewDidFinishLoad") }
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { if navigationAction.targetFrame == nil { webView.load(navigationAction.request) } return nil }
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error){ SVProgressHUD.showError(withStatus:"Wi-Fi Error") }
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error){ SVProgressHUD.showError(withStatus:"Wi-Fi Error") }
func getResult() { //何かする }