ios - Stopping NSURL Connection before redirect, while getting the redirect URL -


i trying response body of url, redirects invalid url. stop redirection well, since crashes app.

i have tried using following methods within code, aren't picked up:

func urlsession(_ session: nsurlsession,     datatask datatask: nsurlsessiondatatask,     didreceiveresponse response: nsurlresponse,     completionhandler completionhandler: (nsurlsessionresponsedisposition) -> void) {         print("did here?") }  func urlsession(_ session: nsurlsession,     task task: nsurlsessiontask,     didcompletewitherror error: nserror?) {         print("how here?") }  func urlsession(_ session: nsurlsession,     task task: nsurlsessiontask,     willperformhttpredirection response: nshttpurlresponse,     newrequest request: nsurlrequest,     completionhandler completionhandler: (nsurlrequest?) -> void) {         print("maybe here?") }  func urlsession(_ session: nsurlsession,     didbecomeinvalidwitherror error: nserror?) {         print("did find error?") } 

any ideas on can do?

edit: so, updates code have gives me no errors using xcode 7:

class example: nsobject, nsurlsessiondelegate, nsurlsessiontaskdelegate {   override init() {     super.init()      let mysession = nsurlsession(configuration: nsurlsessionconfiguration.ephemeralsessionconfiguration(), delegate: self, delegatequeue: nsoperationqueue.mainqueue())      // put handler second parameter.     let data = try!mysession.datataskwithurl(nsurl(string: "http://www.google.com")!, completionhandler: myhandler)      data!.resume() }  // create handler following signature, , read response body. func myhandler(data: nsdata?, response: nsurlresponse?, error: nserror?) -> void {      // in case “encoding” nsasciistringenconding. depends on website.     let responsebody = nsstring(data: data!, encoding: nsasciistringencoding)      print(responsebody) }    // handles redirection private func urlsession(session: nsurlsession, task: nsurlsessiontask, willperformhttpredirection response: nshttpurlresponse, newrequest request: nsurlrequest, completionhandler: (nsurlrequest!) -> void) {      // stops redirection, , returns (internally) response body.     completionhandler(nil) } 

}

to stop redirect , response body should call completionhandler nil parameter.

after stop redirect, urlsession call handler specified, when created http request datataskwithurl:completionhandler:(could datataskwithrequest:completionhandler:). in handler responde body. example:

import foundation  class example: nsobject, nsurlsessiondelegate, nsurlsessiontaskdelegate {       override init() {         super.init()          let mysession = nsurlsession(configuration: nsurlsessionconfiguration.ephemeralsessionconfiguration(), delegate: self, delegatequeue: nsoperationqueue.mainqueue())          // put handler second parameter.         let data = mysession.datataskwithurl(nsurl(string: "http://www.google.com")!, completionhandler: myhandler)          data.resume()     }      // create handler following signature, , read response body.     func myhandler(data: nsdata!, response: nsurlresponse!, error: nserror!) -> void {          // in case “encoding” nsasciistringenconding. depends on website.         let responsebody = nsstring(data: data, encoding: nsasciistringencoding)          println(responsebody)     }        // handles redirection     func urlsession(session: nsurlsession, task: nsurlsessiontask, willperformhttpredirection response: nshttpurlresponse, newrequest request: nsurlrequest, completionhandler: (nsurlrequest!) -> void) {          // stops redirection, , returns (internally) response body.         completionhandler(nil)     } } 

apple reference: https://developer.apple.com/library/ios/documentation/foundation/reference/nsurlsessiontaskdelegate_protocol/index.html#//apple_ref/occ/intfm/nsurlsessiontaskdelegate/urlsession:task:willperformhttpredirection:newrequest:completionhandler:

i know works http 302 code, others 301 (moved permanently) or 308 don't know if works.

worth adding (about urlsession:task:willperformhttpredirection:newrequest:completionhandler:):

this method called tasks in default , ephemeral sessions. tasks in background sessions automatically follow redirects.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -