WKweb视图无法正确加载?

时间:2017-09-29 18:51:22

标签: ios swift wkwebview

enter image description here类WebViewController:UIViewController,WKUIDelegate {     var _responseData:Data?

@IBOutlet weak var webViewMain: WKWebView!
var language: Int = 0
var movieName = String()
var urlStrng = String()
var urlMain: URL?
var data: Data?
var fileName = String()
var queue = OperationQueue()
 var pathOriginal = String()
var pathDuplocate = String()
var refreshAlert = UIAlertController()
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    //creating music folder this plays key role
    webViewMain.translatesAutoresizingMaskIntoConstraints = false
    let height = NSLayoutConstraint(item: webViewMain, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0)
    let width = NSLayoutConstraint(item: webViewMain, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, constant: 0)
    view.addConstraints([height, width])
    let webConfiguration = WKWebViewConfiguration()
    webViewMain = WKWebView(frame: .zero, configuration: webConfiguration)
    webViewMain.uiDelegate = self
    webViewMain.navigationDelegate = self
    view = webViewMain
    switch language {
    case 1:
        urlStrng = "https://cse.google.co.in/cse?cx=005819264335345607731:ta3f5zeiqpm&q=king&oq=king&gs_l=partner.3...0.0.1.177130.0.0.0.0.0.0.0.0..0.0.gsnos%%2Cn%%3D13...0.0..1ac..25.partner..3.1.288.ArU-FOKoCOI#gsc.tab=0&gsc.q=\(movieName)&gsc.page=1"
        print("\(urlStrng)")
    case 2:
         urlStrng = "https://cse.google.com/cse?cx=017716760286974932527%%3Aa5xw-xxsaic&q=vikram%%20veda&oq=vikram%%20veda&gs_l=partner.3..0.3535.6702.0.8294.11.7.0.3.3.0.688.2733.1j1j0j2j1j2.7.0.gsnos%%2Cn%%3D13...0.3027j1482321j11..1ac.1.25.partner..4.7.1281.Ja2pFYDKTV4#gsc.tab=0&gsc.q=\(movieName)%%20&gsc.page=1"
        print("\(urlStrng)")
    case 3:
        //hindi
         urlStrng = "https://www.songsmp3.co/category/search?search=\(movieName)"
        print("\(urlStrng)")
    case 4:
        //malayala
         urlStrng = "https://cse.google.com/cse?cx=017716760286974932527%%3Aa5xw-xxsaic&q=vikram%%20veda&oq=vikram%%20veda&gs_l=partner.3..0.3535.6702.0.8294.11.7.0.3.3.0.688.2733.1j1j0j2j1j2.7.0.gsnos%%2Cn%%3D13...0.3027j1482321j11..1ac.1.25.partner..4.7.1281.Ja2pFYDKTV4#gsc.tab=0&gsc.q=\(movieName)%%20&gsc.page=1"
        print("\(urlStrng)")
    case 5:
        //malayala
         urlStrng = "https://cse.google.com/cse?cx=017716760286974932527%%3Aa5xw-xxsaic&q=vikram%%20veda&oq=vikram%%20veda&gs_l=partner.3..0.3535.6702.0.8294.11.7.0.3.3.0.688.2733.1j1j0j2j1j2.7.0.gsnos%%2Cn%%3D13...0.3027j1482321j11..1ac.1.25.partner..4.7.1281.Ja2pFYDKTV4#gsc.tab=0&gsc.q=\(movieName)%%20&gsc.page=1"
        print("\(urlStrng)")
    default:break

    }
    print("\(urlStrng)")
     //urlMain = URL(string:urlStrng)
    let url : NSString = urlStrng as NSString
    let urlStr : NSString = url.addingPercentEscapes(using: String.Encoding.utf8.rawValue)! as NSString
    let searchURL : NSURL = NSURL(string: urlStr as String)!
    print(searchURL)
    let request = URLRequest(url:searchURL as URL)
    webViewMain.load(request)

}


 extension WebViewController:WKNavigationDelegate
{
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {

    print(error.localizedDescription)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    print( webView.url)
    print("Strat to load")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    print("finish to load")
}
public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)
{
    print(error.localizedDescription)
}
 public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void)
 { if (navigationAction.navigationType == .linkActivated){
    decisionHandler(.allow)
 } else {
    decisionHandler(.cancel)
    }
    print("decidepolicy")
}
public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!)
{

        print("didreciveserver")

}

 public func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!)
{

     print("didCommit")

}


    public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void)
   {
         print("didreciveChalange")
}
@available(iOS 9.0, *)
 public func webViewWebContentProcessDidTerminate(_ webView: WKWebView)
{
     print("didTerminate")

}

它没有加载显示2个错误的网页视图 - :

  

1。)由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'完成处理程序已通过   to - [WingsMusic.WebViewController   webView:didReceiveAuthenticationChallenge:completionHandler:]不是   所谓的'然后我删除了完成delegete并加载网页   第一次再次出现以下错误

     

2.)无法发出服务信号com.apple.WebKit.Networking:113:找不到指定的服务

1 个答案:

答案 0 :(得分:0)

在任何情况下,您都应该为decisionPolicyFor navigationAction返回decisionHandler。否则,你可以看到上面的崩溃