ios - HTTP request in swift 3 Xcode 8.3 -
i getting stuck http request.it did not show error.compiler reads first 2 lines , skip code "task.resume()".i fetching data same code on other view controller creats problem here
func getcustomers() { let url = nsurl(string: "myurl.com") let task = urlsession.shared.datatask(with: url! url) { (data, response, error) in guard let _:data = data, let _:urlresponse = response , error == nil else { print("error: \(string(describing: error))") return } { self.getcustomersarray = [getcustomers]() //json parsing if let data = data, let json = try jsonserialization.jsonobject(with: data) as? [string: any] { let results = json["result"] as? [[string : any]] let getcustomersobject:getcustomers = getcustomers() result in results! { getcustomersobject.activityprefix = (result["activityprefix"] as? string)! getcustomersobject.customerid = (result["customerid"] as? string)! getcustomersobject.customername = (result["customername"] as? string)! getcustomersobject.tfmcustomerid = (result["tfmcustomerid"] as? string)! getcustomersobject.shortname = (result["shortname"] as? string)! getcustomersobject.userrights = (result["userrights"] as? int)! self.totalcustomers += self.totalcustomers } self.customername = getcustomersobject.customername } }//end catch { } } task.resume() }
using block get/post/put/delete:
let request = nsmutableurlrequest(url: url(string: "your api url here" ,param: param))!, cachepolicy: .useprotocolcachepolicy, timeoutinterval:"your request timeout time in seconds") request.httpmethod = "get" request.allhttpheaderfields = headers as? [string : string] let session = urlsession.shared let datatask = session.datatask(with: request urlrequest) {data,response,error in let httpresponse = response as? httpurlresponse if (error != nil) { print(error) } else { print(httpresponse) } dispatchqueue.main.async { //update ui here } } datatask.resume()
i think dont mention line request.httpmethod = "get"
Comments
Post a Comment