2017-09-25 139 views
0

我正在應用程序中實現URLProtocol。URLProtocol未初始化

import Cocoa 

class MyURLProtocol: URLProtocol { 

    override init(request: URLRequest, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) { 
     super.init(request: request, cachedResponse: cachedResponse, client: client) 
    } 

    override class func canInit(with request: URLRequest) -> Bool { 
     return true 
    } 

    override class func canonicalRequest(for request: URLRequest) -> URLRequest { 
     return request 
    } 

    override func startLoading() { 
     print("loading") 
    } 
} 

雖然canInit(with request: URLRequest)總是返回true,既不init(…)也不canonicalRequest(…)也不startLoading()被調用。

URLProtocol.registerClassMyURLProtocolAppDelegate

我不知道該怎麼做叫willFinishLaunching。昨天,代碼至少調用了函數。

感謝您的幫助。

回答

0

您使用的是URLSessionURLSession跳過了正常的協議註冊,而是您明確地配置了URLSessionConfiguration中的協議。見URLSessionConfiguration.protocolClasses

+0

不,我只在WKWebView中加載URLRequest。 – Sandro

+0

我需要在我的應用程序中處理不同的url模式 – Sandro

+0

我還實現了WKNavigationDelegate和func webView(_ webView:WKWebView,didFailProvisionalNavigation導航:WKNavigation !, withError錯誤:錯誤)被調用時出現不受支持的url錯誤。 – Sandro

0

其實我是在一個macOS應用程序上工作,而不是一個iOS應用程序,但它已經解決了我從WKWebView更改爲WebView時的問題。

感謝Kevin Ballard對他的評論。

+0

iOS 11 API也出現在macOS 10.13中(今天發佈)。 –