swift - NSURLSession validation -


i'm trying o work out correct (or @ least good) way handle data request using swift 2.

what want achieve to:

  1. check if data connection available, if no alert user
  2. if connection available make request
  3. check request response, if invalid alert user
  4. if valid, check response code , display 1 of 2 messages

what have far

    let requesturl: string = **********************     let url = nsurl(string: requesturl)!     let request = nsmutableurlrequest(url: url)     let session = nsurlsession.sharedsession()      let postparams: nsdictionary = ["id":newid!]      request.httpmethod = "post"      {         let jsondata = try nsjsonserialization.datawithjsonobject(postparams, options: [])         let jsonstring = nsstring(data: jsondata, encoding: nsutf8stringencoding)! string         let utf8str = jsonstring.datausingencoding(nsutf8stringencoding)         let base64encoded = utf8str?.base64encodedstringwithoptions(nsdatabase64encodingoptions(rawvalue: 0))         let bodydata = "data="+base64encoded!          request.httpbody = bodydata.datausingencoding(nsutf8stringencoding)     } catch {         print("error serialising json")     }      session.datataskwithrequest(request) { data, response, error in          // make sure response ok         guard let realresponse = response as? nshttpurlresponse realresponse.statuscode == 200 else         {             // show success message             print("invalid response server")         }          // read json data         if let poststring = nsstring(data:data!, encoding: nsutf8stringencoding) as? string {             if let responsedata = poststring.datausingencoding(nsutf8stringencoding) {                 let json = json(data: responsedata)                 let responsemessage:string = json["response"]["data"].stringvalue                 if(responsemessage == "success")                 {                    print("successful post")                 }else{                    print("error saving post")                 }              }         }     } 

currently if run in airplane mode nothing happens, catch , tell these need data connection

try sth this:

func connectedtonetwork() -> bool {             var zeroaddress = sockaddr_in()             zeroaddress.sin_len = uint8(sizeofvalue(zeroaddress))             zeroaddress.sin_family = sa_family_t(af_inet)             let defaultroutereachability = withunsafepointer(&zeroaddress) {                 scnetworkreachabilitycreatewithaddress(nil, unsafepointer($0))             }             var flags = scnetworkreachabilityflags()             if !scnetworkreachabilitygetflags(defaultroutereachability!, &flags) {                 return false             }             let isreachable = (flags.rawvalue & uint32(kscnetworkflagsreachable)) != 0             let needsconnection = (flags.rawvalue & uint32(kscnetworkflagsconnectionrequired)) != 0             return (isreachable && !needsconnection)             } 

if(connectedtonetwork() == true){ //call function make request } else { //show alert need internet connection } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -