ios - Can't get HTTP basic Auth working -


i'm trying call local rest service uses http basic auth.

as result of request following:

found: error - unauthorized
found: reason - password required

here function doing request.

func connect(url: string) -> bool {      let url: nsurl = nsurl(string: url);     let login:string = "admin";     let password:string = "test123";      var defaultcredentials: nsurlcredential = nsurlcredential(user: login, password: password, persistence: nsurlcredentialpersistence.forsession);      let host: string = url.host;     let port: int = url.port;     let prot: string = url.scheme;      println("set following vars \(host) + \(port) + \(prot)");      var protectionspace: nsurlprotectionspace = nsurlprotectionspace(host: host,port: port,`protocol`: prot,realm: nil,authenticationmethod: nsurlauthenticationmethodhttpbasic);      var credentialstorage: nsurlcredentialstorage = nsurlcredentialstorage.sharedcredentialstorage();     credentialstorage.setcredential(defaultcredentials, forprotectionspace: protectionspace);      var sessionconfiguration: nsurlsessionconfiguration = nsurlsessionconfiguration.defaultsessionconfiguration();     sessionconfiguration.urlcredentialstorage = credentialstorage;      let session: nsurlsession = nsurlsession(configuration: sessionconfiguration); //nsurlsession.sharedsession()       //nsurlsession.sessionwithconfiguration(sessionconfiguration);      let task = session.datataskwithurl(url, completionhandler: {data, response, error -> void in         println("task completed")         if((error) != nil) {             // if there error in web request, print console             println(error.localizeddescription)         }         var err: nserror?         var jsonresult = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &err) nsdictionary         if(err != nil) {             // if there error parsing json, print console             println("json error \(err!.localizeddescription)")         }           self.delegate.didreceiveapiresults(jsonresult);      })     task.resume()      return true } 

could please give me hint?

i consider using afnetworking. tried using alamofire ran issue basic auth (more info on here). used basic auth in swift.

    static let sharedinstance = networkmanager(url: nsurl(string: baseurl))     ...     sharedinstance.requestserializer.clearauthorizationheader()     sharedinstance.requestserializer.setauthorizationheaderfieldwithusername(email, password:password)     sharedinstance.post(loginapi,         parameters: nil,         success: success,         failure: failure) 

note success , failure closures (like blocks if coming objective c) passing method.

if haven't imported objective c library swift yet, recommend using cocoapods would. add line bridging-header.h file.

#import <afnetworking/afnetworking.h> 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -