2017-12-08 8 views
-1

WKWebViewベースのiOSアプリケーションで問題が発生しています。ページ上のリンクの一部はクリックできません。私がSafariのView Controllerで試してみると、彼らは機能します。誰でも助けてくれますか?一部のWKWebViewリンクをクリックできない

これは私のソースコードです:

import UIKit 
import WebKit 

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate { 
    var webView: WKWebView! //declare the webview has to be optional 
    var activityIndicator: UIActivityIndicatorView! 


    override func loadView() { 
     super.loadView() 
     let webConfiguration = WKWebViewConfiguration() 
     webView = WKWebView(frame: .zero, configuration: webConfiguration) 
     webView.uiDelegate = self 
     webView.navigationDelegate = self 
     view = webView 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let myURL = URL(string: "https://stolenandfound.com/") 
     let myRequest = URLRequest(url: myURL!) 
     webView.load(myRequest) 


     activityIndicator = UIActivityIndicatorView() //declare the progress indicator 
     activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 

     self.activityIndicator.center = CGPoint(x:self.view.bounds.size.width/2.0,y: self.view.bounds.size.height/2.0); 

     activityIndicator.autoresizingMask = (UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleRightMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleLeftMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleBottomMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleTopMargin.rawValue)))) 

     //activityIndicator.center = CGPoint(x: UIScreen.main.bounds.size.width/2, y: UIScreen.main.bounds.size.height/2) 
     activityIndicator.hidesWhenStopped = true 

     activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge 
     activityIndicator.color = UIColor.darkGray 
     self.view.addSubview(activityIndicator) 
    } 



    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { 
     activityIndicator.startAnimating() 
    } 

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { 
     print("it is an error") 
     activityIndicator.stopAnimating() 
     let alert = UIAlertController(title: "Network Error", message: "You have no internet coonection", preferredStyle: .alert) 

     let restartAction = UIAlertAction(title: "Reload page", style: .default, handler: { (UIAlertAction) in 
      self.viewDidLoad() 
     }) 

     alert.addAction(restartAction) 
     present(alert, animated: true, completion: nil) 
    } 




    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { 
     activityIndicator.stopAnimating() 
    } 

    @IBAction func backButton(_ sender: UIBarButtonItem) { 
     webView.goBack() 
    } 


    @IBAction func refreshButton(_ sender: UIBarButtonItem) { 
     webView.reload() 
    } 

} 

答えて

0

Safariで新しいタブでリンクが開いている場合は、WKUIDelegate機能func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?を実装し、そこにリクエストを処理する必要があります。その時点で、既存のWebViewでリクエストをロードしたり、新しいWebViewを作成したり、Safariで開くことができます。

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { 
     guard let theURL = navigationAction.request.url else { 
      return nil 
     } 
     if loadInCurrentWebview{ 
      _ = webView.load(navigationAction.request) //loads the link in the current webView 
     } else if loadInNewWebview{ 
      return WKWebView() 
     }else { 
      //loads in safari 
      if #available(iOS 10.0, *) { 
       UIApplication.shared.open(theURL, options: [:], completionHandler: nil) 
      } else { 
       UIApplication.shared.openURL(theURL) 
      } 
     } 
    return nil 
} 
関連する問題