Swift 3:UrlProtocol didCompleteWithError委托永远不会被调用

时间:2017-02-01 06:23:42

标签: ios macos nsurlprotocol url-protocol custom-url-protocol

作为标题,我正在尝试构建自定义URL协议。 我找到了this,我完全遵循了提供的代码。

然而,这个代表

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
永远不会触发或调用

此外,编译器提示一条警告消息,表示它几乎与 URLSessionTaskDelegate 的选项要求相匹配,编译器提供的快速修复是将其设为私有。

那么,如何调用 didCompleteWithError 委托,here中提供的代码是否遗漏了某些部分?或者这是一个已知的问题?如果有任何解决方案或更好的Swift 3自定义UrlProtocol示例,请告诉我。在此先感谢!,

编辑1:

class CustomURLProtocol: URLProtocol, URLSessionDataDelegate, URLSessionTaskDelegate {
private var dataTask: URLSessionDataTask?
private var urlResponse: URLResponse?
private var receivedData: NSMutableData?

class var CustomHeaderSet: String {
    return "CustomHeaderSet"
}

// MARK: NSURLProtocol

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

    if (URLProtocol.property(forKey: CustomURLProtocol.CustomHeaderSet, in: request as URLRequest) != nil) {
        return false
    }

    return true
}

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

override func startLoading() {

    let mutableRequest =  NSMutableURLRequest.init(url: self.request.url!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 240.0)//self.request as! NSMutableURLRequest

    //Add User Agent

    var userAgentValueString = "myApp"
    mutableRequest.setValue(userAgentValueString, forHTTPHeaderField: "User-Agent")

    print(mutableRequest.allHTTPHeaderFields ?? "")
    URLProtocol.setProperty("true", forKey: CustomURLProtocol.CustomHeaderSet, in: mutableRequest)
    let defaultConfigObj = URLSessionConfiguration.default
    let defaultSession = URLSession(configuration: defaultConfigObj, delegate: self, delegateQueue: nil)
    self.dataTask = defaultSession.dataTask(with: mutableRequest as URLRequest)
    self.dataTask!.resume()
    print("loaded")
}

override func stopLoading() {
    self.dataTask?.cancel()
    self.dataTask       = nil
    self.receivedData   = nil
    self.urlResponse    = nil
}

// MARK: NSURLSessionDataDelegate

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask,
                didReceive response: URLResponse,
                completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {

    self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)

    self.urlResponse = response
    self.receivedData = NSMutableData()

    completionHandler(.allow)
}

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
    self.client?.urlProtocol(self, didLoad: data as Data)

    self.receivedData?.append(data as Data)
}

// MARK: NSURLSessionTaskDelegate

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {

    print("completed")
    if error != nil { //&& error.code != NSURLErrorCancelled {
        self.client?.urlProtocol(self, didFailWithError: error! as! Swift.Error)
    } else {
        //saveCachedResponse()
        self.client?.urlProtocolDidFinishLoading(self)
    }
}

}

当代码发布在那里时,那些是我所做的更改,'loaded'被调用但是'completed'永远不会被调用。

编辑2:

编译器提示的警告消息 nearly matches optionals requirement of URLSessionTaskDelegate

0 个答案:

没有答案