我正在尝试使用Swift扩展为委托方法编写默认行为,如下所示,但它永远不会被调用。有谁知道为什么或如何以正确的方式做到这一点?
extension NSURLSessionDelegate {
public func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
//default behaviour here
}
}
添加override
也不起作用。
根据this, 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(如下面的评论中所述)。
答案 0 :(得分:2)
您可以扩展NSURLSessionDelegate
协议以添加默认实现,但您的NSURLSession
对象需要代理。
此委托只能使用+sessionWithConfiguration:delegate:delegateQueue:
设置(因为委托属性为read only),所以设置它的唯一方法是子类NSURLSession
,覆盖+sessionWithConfiguration:
并使用delegate属性调用初始化程序。这里的问题是您必须将所有NSURLSession
个对象替换为MyCustomSessionClass
。对象。
我建议您创建一个符合NSURLSessionDelegate
协议的SessionCreator类,并创建NSURLSession
个对象。您仍然需要替换对象的创建,但至少该对象不是自己的委托。
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())