3

我正在尝试使用如下的 Swift 扩展为委托方法编写默认行为,但它从未被调用。有谁知道为什么或如何以正确的方式做到这一点?

extension NSURLSessionDelegate {

    public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        //default behaviour here
    }
}

添加override也不起作用。

据此,Apple 的默认实现如下所示

extension NSURLSessionDelegate {
    func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) { }
    func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) { }
}

我的 DataTask 调用通常如下所示:

let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
    sessionConfiguration.HTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
let session = NSURLSession(configuration: sessionConfiguration)
let requestURL = NSURL(string:"https:www.google.com/blabla")

session.dataTaskWithURL(requestURL!, completionHandler: completion).resume()

哪里completion通常是通过参数接收的 Swift 闭包。

我需要为整个应用程序URLSession(... didReceiveChallenge ...)中的所有实现实现该功能nsurlsessiontask,但由于我需要使用 completionHandler(如下面的评论中所述),因此无法设置我的会话委托。

4

1 回答 1

2

您可以扩展NSURLSessionDelegate协议以添加默认实现,但您的NSURLSession对象需要委托。

只能使用设置此委托+sessionWithConfiguration:delegate:delegateQueue:(因为委托属性是只读的),因此设置它的唯一方法是子类NSURLSession,覆盖 +sessionWithConfiguration:并使用委托属性调用初始化程序。这里的问题是您必须将所有NSURLSession对象替换为MyCustomSessionClass. 对象。

我建议您创建一个符合NSURLSessionDelegate协议并创建NSURLSession对象的 SessionCreator 类。您仍然必须替换对象的创建,但至少该对象不是其自身的委托。

public class SessionCreator:NSObject,NSURLSessionDelegate {

    //MARK: - Singleton method    
    class var sharedInstance :SessionCreator {
        struct Singleton {
            static let instance = SessionCreator()
        }
        return Singleton.instance
    }

    //MARK: - Public class method    
    public class func createSessionWithConfiguration (configuration:NSURLSessionConfiguration) -> NSURLSession {
        return sharedInstance.createSessionWithConfiguration(configuration)
    }

    //MARK: - Private methods
    private func createSessionWithConfiguration (configuration:NSURLSessionConfiguration) -> NSURLSession {
        return NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
    }

    //MARK: - NSURLSessionDelegate protocol conformance
    public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        // Always called since it's the delegate of all NSURLSession created using createSessionWithConfiguration
    }
}

// Let create a NSURLSession object :
let session = SessionCreator.createSessionWithConfiguration(NSURLSessionConfiguration())
于 2016-03-04T13:09:19.263 回答