ios - Swift - NSURLSession for Windows Authentication -


i have class here , inside class method , trying nsurlsession on api requires windows authentication username , password. have followed tutorial here https://gist.github.com/n8armstrong/5c5c828f1b82b0315e24

and came this:

let webservice = "https://api.com"  let config = nsurlsessionconfiguration.defaultsessionconfiguration() let urlsession = nsurlsession(configuration: config)  class webservice: nsobject {      func loginuser(username: string, password: string) -> bool {          let userpasswordstring = "username@domain.com:password"         let userpassworddata = userpasswordstring.datausingencoding(nsutf8stringencoding)         let base64encodedcredential = userpassworddata!.base64encodedstringwithoptions([])         let authstring = "basic \(base64encodedcredential)"          config.httpadditionalheaders = ["authorization" : authstring]          let requeststring = nsstring(format:"%@", webservice) string         let url: nsurl! = nsurl(string: requeststring)          let task = urlsession.datataskwithurl(url) {             (let data, let response, let error) in             if (response as? nshttpurlresponse) != nil {                 let datastring = nsstring(data: data!, encoding: nsutf8stringencoding)                 print(datastring)             }         }          task.resume()          return true      }  } 

but when run 401 error: 401 - unauthorized: access denied due invalid credentials.

i have confirmed url api correct. same username , password. doing wrong?

i able fix doing following:

var credential: nsurlcredential!      func loginuser(username: string, password: string) -> bool {          let configuration = nsurlsessionconfiguration.defaultsessionconfiguration()         let session = nsurlsession(configuration: configuration, delegate: self, delegatequeue: nil)          credential = nsurlcredential(user:username, password:password, persistence: .forsession)          let requeststring = nsstring(format:"%@", webservice) string         let url: nsurl! = nsurl(string: requeststring)          let task = session.datataskwithurl(url, completionhandler: {             data, response, error in              dispatch_async(dispatch_get_main_queue(),             {                  if(error == nil)                 {                     print("yay!")                 }                 else                 {                     print("naw!")                 }              })          })          task.resume()          return true      } 

and adding nsurlsessiondelegate methods:

func urlsession(session: nsurlsession, didreceivechallenge challenge: nsurlauthenticationchallenge, completionhandler: (nsurlsessionauthchallengedisposition, nsurlcredential?) -> void) {          if challenge.previousfailurecount > 0         {             completionhandler(nsurlsessionauthchallengedisposition.cancelauthenticationchallenge, nil)         }         else         {             completionhandler(nsurlsessionauthchallengedisposition.usecredential, nsurlcredential(fortrust:challenge.protectionspace.servertrust!))         }      }      func urlsession(session: nsurlsession, task: nsurlsessiontask, didreceivechallenge challenge: nsurlauthenticationchallenge, completionhandler: (nsurlsessionauthchallengedisposition, nsurlcredential?) -> void) {          completionhandler(nsurlsessionauthchallengedisposition.usecredential,credential)      } 

Comments