0

作为标题,我正在尝试构建自定义 url 协议。我发现了这一点,并完全遵循了提供的代码。

然而,这位代表

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

永远不会被触发或调用。

此外,编译器提示一条警告消息说它几乎符合URLSessionTaskDelegate的可选要求,编译器提供的快速修复是使其私有。

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

编辑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)
    }
}

}

正如那里发布的代码,这些是我所做的更改,“加载”被调用,但“完成”从未被调用。

编辑2:

编译器提示的警告信息 几乎符合 URLSessionTaskDelegate 的选项要求

4

0 回答 0