2014-08-28 166 views
1

我正在嘗試使用NSURLConnection的示例項目。didReceiveAuthenticationChallenge委託沒有在Swift中調用

import Foundation 
import UIKit 
    class loginVC: ViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSURLConnectionDataDelegate{ 
     var webData: NSMutableData! 

     override func viewDidLoad() { 
      super.viewDidLoad() 
      webData = NSMutableData() 
      callWebService("[email protected]", Password:"1") 

     } 

     func callWebService(userName:NSString, Password:NSString){ 

      var strURl: NSURL = NSURL .URLWithString("") 
      var request: NSMutableURLRequest = NSMutableURLRequest(URL: strURl, cachePolicy:NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval:60.0) 
      var postString: NSString = "" 
      postString = postString.stringByAppendingFormat("username=%@&password=%@", userName,Password) 
      request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 
      request.HTTPMethod = "" 
      var connection: NSURLConnection = NSURLConnection(request: request, delegate:self) 
      connection.start() 
     } 

     //NSURLConnection webservice 
     func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!){ 
      // webData.length = 0 
      println("response") 
     } 

     func connection(connection: NSURLConnection!, didReceiveData data: NSData!){ 
      println(data.length) 
      webData .appendData(data) 
     } 

     func connection(connection: NSURLConnection, didFailWithError error: NSError!){ 
      println("error in connection") 
     } 

     func connectionDidFinishLoading(connection: NSURLConnection!){ 

      var response: NSString = NSString(data: webData, encoding: NSUTF8StringEncoding) 

      println("response:\(response)") 
      if response != ""{ 

      } 

     } 
     func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge!){ 
      var authentication: NSURLCredential = NSURLCredential.credentialWithUser("", password:"", persistence: NSURLCredentialPersistence.ForSession) 
     } 
    } 

似乎所有代表都獲取調用除了didReceiveAuthenticationChallenge代表method.What我是missing.any幫助將appreciated.thanks提前

+0

您不需要在連接上調用start() - 只有在使用'initWithRequest:delegate:startImmediately:'並傳遞false來立即啓動時才需要'start()'。 – KerrM 2014-08-28 08:58:49

回答

1

對於iOS 8.0及以上,必須實現connection(_:willSendRequestForAuthenticationChallenge:)。在iOS8中不會調用connection:didReceiveAuthenticationChallenge:,只能在較早的操作系統中調用。

所以,在iOS8上提供認證及以上,實現上面的方法,並在那裏你必須調用的挑戰 - 應答方法之一(NSURLAuthenticationChallengeSender protocol):

useCredential:forAuthenticationChallenge: 

continueWithoutCredentialForAuthenticationChallenge: 

cancelAuthenticationChallenge: 

performDefaultHandlingForAuthenticationChallenge: 

rejectProtectionSpaceAndContinueWithChallenge: 

didReceiveAuthenticationChallenge是方法上NSURLConnectionDelegate,而其餘的方法(didFailWithError除外)均爲NSURLConnectionDataDelegate方法。你是否在你的控制器中實現了兩種協議?如果你發佈了所有班級的代碼,這可能會有所幫助。

+0

我已經實現了兩個協議,但它沒有被調用? – user3823935 2014-08-28 08:40:47

+0

請顯示你的全班。 – KerrM 2014-08-28 08:42:25

+0

我更新了我的代碼.. – user3823935 2014-08-28 08:51:05

0

我不認爲這個問題是在這些方面新雨燕3.0 ..

我有類似的問題,一個代碼下xcode7/ios7-8-9 /雨燕2.2

定期工作遷移已經產生:

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: (Foundation.URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { 

... }

但不工作。

重寫手:

func urlSession(_: URLSession, 
       didReceive challenge: URLAuthenticationChallenge, 
       completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { 


} 

現在它的工作。 我的兩毛錢。

相關問題