iphone - How to apply Cookie on REST web service URL using iOS Swift -
i have made rest request application, , able data properly. want apply authentication cookie on web service url. how can this. please help. below current code.
callrestparser.swift - in processrequest method called view controller. processrequest method receive web service url , send request restparser class. getreceivedata delegate method called after successful connection.
protocol updateprojectuidelegate{ func updateprojectui(data:nsdictionary) } class callrestparser: nsobject, restparserdelegate { var rest1=restparser() var projectdelegate: updateprojectuidelegate? func processrequest(url:string) { var nsurl = nsurl(string: url) var createrequest: nsmutableurlrequest = nsmutableurlrequest(url: nsurl!) createrequest.httpmethod = "get" rest1.delegate = self rest1.httprequest(createrequest) } func getreceivedata(recvdata:nsmutabledata,sender:restparser){ var error: nserror? var jsonresult: nsdictionary! = nsjsonserialization.jsonobjectwithdata(recvdata, options:nsjsonreadingoptions.mutablecontainers, error: &error) as? nsdictionary if error != nil{ print("error occured \(error?.localizedfailurereason)") return } println(jsonresult.count) projectdelegate?.updateprojectui(jsonresult) //var updateui = projectdetails_tab() //updateui.updateui() } }
restparser.swift -
protocol restparserdelegate{ func getreceivedata(data:nsmutabledata,sender:restparser) } class restparser: nsobject, nsurlconnectiondatadelegate { var receivedata: nsmutabledata! var requestconnection: nsurlconnection! var delegate: restparserdelegate? func receivedata(resdata:nsmutabledata){ receivedata = resdata } func requestconnection(reqconn:nsurlconnection){ requestconnection = reqconn } func httprequest(myrequest:nsmutableurlrequest){ self.requestconnection = nsurlconnection(request: myrequest, delegate: self) } // nsurlconnectiondatadelegate methods func connection(connection: nsurlconnection, didreceiveresponse response: nsurlresponse){ self.receivedata = nsmutabledata() } func connection(connection: nsurlconnection, didreceivedata data: nsdata){ self.receivedata.appenddata(data) } func connectiondidfinishloading(connection: nsurlconnection){ //if let revdata = receivedata{ self.delegate?.getreceivedata(receivedata, sender: self) //}else //{ // nslog("no data : nil") //} self.delegate = nil self.receivedata = nil self.requestconnection = nil } func connection(connection: nsurlconnection, didfailwitherror error: nserror){ nslog("failed error - %@ ",error.localizeddescription) } func connection(connection: nsurlconnection, canauthenticateagainstprotectionspace protectionspace: nsurlprotectionspace) -> bool{ return protectionspace.authenticationmethod == nsurlauthenticationmethodservertrust } func connection(connection: nsurlconnection, didreceiveauthenticationchallenge challenge: nsurlauthenticationchallenge){ if(challenge.protectionspace.authenticationmethod == nsurlauthenticationmethodservertrust){ if(challenge.protectionspace.host == "domain.com"){ let credentials = nsurlcredential(fortrust: challenge.protectionspace.servertrust) challenge.sender.usecredential(credentials, forauthenticationchallenge: challenge) } } challenge.sender.continuewithoutcredentialforauthenticationchallenge(challenge) } }
and authentication cookie getting authsessioncookiekeyval - xx-x-session-id=1_2_1_xxxxxxgsag1k5axxxxxxxv . how can apply cookie on above rest call. please help.
you have pass cookie in header
of nsmutableurlrequest
var nsurl = nsurl(string: url) var createrequest: nsmutableurlrequest = nsmutableurlrequest(url: nsurl!) let cookiearray = nsarray(objects: cookie1,cookie2) // cookie1 , cookie2 object of nshttpcookie let cookieheader = nshttpcookie.requestheaderfieldswithcookies(cookiearray) createrequest.allhttpheaderfields = nsdictionary(dictionary: cookieheader)
edit
cookies stored in nshttpcookiestorage
var cookie1:nshttpcookie? // cookie object var cookie2:nshttpcookie? // cookie object let storage = nshttpcookiestorage.sharedhttpcookiestorage() let cookiearray = storage.cookies! tempcookie in cookiearray { if tempcookie.name == "rtfa" // write cookie name want search { cookie1 = tempcookie as? nshttpcookie continue } else if tempcookie.name == "fedauth" // write cookie name want search { cookie2 = tempcookie as? nshttpcookie continue } } if cookie1 != nil && cookie2 != nil { println("cookie found") } else { println("cookie not found") }
Comments
Post a Comment