Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 3:UrlProtocol无法调用完成的错误委托_Ios_Macos_Nsurlprotocol_Url Protocol_Custom Url Protocol - Fatal编程技术网

Ios Swift 3:UrlProtocol无法调用完成的错误委托

Ios Swift 3:UrlProtocol无法调用完成的错误委托,ios,macos,nsurlprotocol,url-protocol,custom-url-protocol,Ios,Macos,Nsurlprotocol,Url Protocol,Custom Url Protocol,作为标题,我正在尝试构建一个自定义url协议。 我找到了,并且完全遵循了提供的代码 然而,这位代表 func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) 永远不会触发或调用 此外,编译器提示一条警告消息说,它几乎符合URLSessionTaskDelegate的可选要求,编译器提供的快速修复方法是将其私有化 那么,我如何调用didpleteWitherRo

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

然而,这位代表

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

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

那么,我如何调用didpleteWitherRor委托,代码是否遗漏了某些部分?或者这是一个已知的问题?请让我知道是否有任何解决方案或更好的自定义UrlProtocol Swift 3示例。提前谢谢

编辑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:

编译器提示的警告消息

您能否显示添加此委托方法的位置。你也为
URLSessionTaskDelegate
设置了委托吗?我在appdelegate的didfishLaunching中添加了它,作为
URLProtocol.registerClass(CustomURLProtocol.self)
我编辑了帖子,并在我使用的代码中添加了尝试使用urlsessiondownloaddelegate我也面临着同样的问题!任何解决方案都将不胜感激。