2016-03-04 102 views
3

我想寫一個使用Swift擴展的委託方法的默認行爲如下,但它永遠不會被調用。有誰知道爲什麼或如何以正確的方式做到這一點?Swift擴展的協議方法的默認實現

extension NSURLSessionDelegate { 

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

添加override也不起作用。

this, 蘋果的默認實現是這樣的:

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通常會通過參數收到了斯威夫特關閉。

我需要在整個應用程序中爲所有nsurlsessiontask實現實現URLSession(... didReceiveChallenge ...)函數,但無法設置會話的委託,因爲我需要使用completionHandler(正如我在下面的評論中提到的那樣)。

+1

如果你在委託中實現了你的方法,那麼這個方法是否被調用?也許委託方法不會被調用。否則你的實現看起來很好。 –

+0

我用'dataTaskWithURL'和一個completionHandler(我不能通過委託來改變其他原因),所以在創建session時我不能爲我的'NSURLSession'設置一個特定的委託 - else completionHandler isn不叫。另見我更新的問題。 – Stephan

回答

2

您可以擴展NSURLSessionDelegate協議以添加默認實現,但您的NSURLSession對象需要委託。

此委託只能通過設置+sessionWithConfiguration:delegate:delegateQueue:(因爲委託財產read only),所以你唯一的辦法將其設置是繼承NSURLSession,覆蓋+sessionWithConfiguration:並調用與委託財產的初始化。這裏的問題是您必須將所有NSURLSession對象替換爲MyCustomSessionClass。對象。

我建議你創建一個SessionCreator類,它將符合NSURLSessionDelegate協議並將創建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()) 
+1

您可以擴展NSURLSessionDelegate協議以添加默認實現,但您的NSURLSession對象需要一個委託。搞定了。 – Stephan

相關問題