When calling PHP service from Swift for iOS 9, results don't display -
i'm new swift , trying set ios app. @ start of app i'm dropping pin on map , sending location php web service. after next 10 iterations through map movement, pin dropped , location gets sent service. looking @ database getting service , getting loaded tables. but, first time calling service time response data can retrieved.
here's view controller:
// // viewcontroller.swift // tracker // // created kendall crouch on 2/6/16. // copyright © 2016 klc computing. rights reserved. // import uikit import mapkit import corelocation class viewcontroller: uiviewcontroller, cllocationmanagerdelegate, mkmapviewdelegate { var places = [dictionary<string, string>()] var hasstarted: bool = false var locationcount = 0 var routeid: nsinteger = -1 var laststep: nsinteger = -1 var markerid: int = -1 var manager:cllocationmanager! @iboutlet weak var mapview: mkmapview! @iboutlet weak var lbllatitude: uilabel! @iboutlet weak var lbllongitude: uilabel! @iboutlet weak var btnstartcontrol: uibarbuttonitem! @iboutlet weak var btnpausecontrol: uibarbuttonitem! @iboutlet weak var btnstopcontrol: uibarbuttonitem! @ibaction func btnstart(sender: anyobject) { btnstartcontrol.enabled = false btnpausecontrol.enabled = true btnstopcontrol.enabled = true manager.startupdatinglocation() } @ibaction func btnpause(sender: anyobject) { manager.stopupdatinglocation() self.btnstartcontrol.enabled = true self.btnpausecontrol.enabled = false hasstarted = false } @ibaction func btnstop(sender: anyobject) { manager.stopupdatinglocation() self.btnstopcontrol.enabled = false self.btnstartcontrol.enabled = true hasstarted = false } override func viewdidload() { super.viewdidload() locationcount = 500 manager = cllocationmanager() manager.delegate = self manager.desiredaccuracy = kcllocationaccuracybest manager.requestwheninuseauthorization() manager.startupdatinglocation() } func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) { locationcount += 1 let userlocation:cllocation = locations[0] let latitude: cllocationdegrees = userlocation.coordinate.latitude let longitude: cllocationdegrees = userlocation.coordinate.longitude lbllatitude.text = string(userlocation.coordinate.latitude) lbllongitude.text = string(userlocation.coordinate.longitude) let latdelta: cllocationdegrees = 0.05 let londelta: cllocationdegrees = 0.05 let span: mkcoordinatespan = mkcoordinatespanmake(latdelta, londelta) let location: cllocationcoordinate2d = cllocationcoordinate2dmake(latitude, longitude) let region: mkcoordinateregion = mkcoordinateregionmake(location, span) mapview.setregion(region, animated: false) let newcoordinate: cllocationcoordinate2d = cllocationcoordinate2dmake(latitude, longitude) let newlocation = cllocation(latitude: newcoordinate.latitude, longitude: newcoordinate.longitude) if locationcount > 10 { hasstarted = false locationcount = 0 } if !hasstarted { hasstarted = true clgeocoder().reversegeocodelocation(newlocation) { (placemarks, error) -> void in var title = "added on \(nsdate())" if error != nil { print(error) } else { if let p = placemarks?[0] { let formattedaddresslines = p.addressdictionary?["formattedaddresslines"] as! nsarray title = formattedaddresslines.componentsjoinedbystring(", ") } } let annotation = mkpointannotation() annotation.coordinate = newcoordinate annotation.title = title self.mapview.addannotation(annotation) self.places.append(["name":title, "lat":"\(newcoordinate.latitude)", "lon":"\(newcoordinate.longitude)"]) nsuserdefaults.standarduserdefaults().setobject(self.places, forkey: "places") let url: nsurl = nsurl(string: "http://tracker.klccomputing.com/updateroute.php")! let request = nsmutableurlrequest(url: url) request.httpmethod = "post" request.cachepolicy = nsurlrequestcachepolicy.reloadignoringcachedata let bodydata = "routeid=\(self.routeid)&routename=\(title)&routedate=\(nsdate())&laststep=\(self.laststep)&latitude=\(newlocation.coordinate.latitude)&longitude=\(newlocation.coordinate.longitude)" request.httpbody = bodydata.datausingencoding(nsutf8stringencoding); let task = nsurlsession.sharedsession().datataskwithrequest(request, completionhandler: { (data, response, error) -> void in print(data) if let httpresponse = response as? nshttpurlresponse { let statuscode = httpresponse.statuscode if statuscode == 200 { { let json = try nsjsonserialization.jsonobjectwithdata(data!, options: .mutablecontainers) as? nsdictionary if let jsondata = json { print("klca\(jsondata)") if let newrouteid: nsinteger = jsondata["routeid"] as? nsinteger { self.routeid = newrouteid if let newlaststep: nsinteger = jsondata["laststep"] as? nsinteger { self.laststep = newlaststep print("klcb\(self.laststep)") } } } } catch { let responsestring = nsstring(data: data!, encoding: nsutf8stringencoding) print("raw response: \(responsestring)") } } } }) task.resume() } } } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
console information. let app run 3 iterations. first time through variable if statement information variable jsondata worked. second , third times through, didn't work:
optional(<7b22726f 75746549 64223a31 362c226d 61726b65 72496422 3a36312c 226c6173 74537465 70223a31 7d>) klca{ laststep = 1; markerid = 61; routeid = 16; } klcb1 optional(<7b22726f 75746549 64223a22 3136222c 226d6172 6b657249 64223a36 322c226c 61737453 74657022 3a327d>) klca{ laststep = 2; markerid = 62; routeid = 16; } optional(<7b22726f 75746549 64223a22 3136222c 226d6172 6b657249 64223a36 332c226c 61737453 74657022 3a327d>) klca{ laststep = 2; markerid = 63; routeid = 16; }
Comments
Post a Comment