I am currently developing an iOS application that allows users to tap TableViewCells to vote for a song.
I have successfully been able to load the tableviewcells with parsed JSON objects (songs) from a PHP script connecting to a mySQL table with the songs in it.
I have also successfully posted a query to the database using POST method.
However, now I am trying to utilize a POST variable in my query that returns JSON objects for me to parse.
Below is my script for update the database utilizing POST method:
let queryID = self.partyID[0]
let queryGenre = self.recievedGenre
var loadIntoVotes = "queryID=\(queryID)&queryGenre=\(queryGenre)"
let URL: NSURL = NSURL(string: "http://myserverURL/loadSongsIntoTable.php")!
let request:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
request.HTTPMethod = "POST"
request.HTTPBody = loadIntoVotes.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{
(response, data, error) in
}
And here is my script for parsing returned JSON objects:
let url=NSURL(string:"http://myserverURL/grabID.php")
let allSongsData=NSData(contentsOfURL:url!)
var allSongs: AnyObject! = NSJSONSerialization.JSONObjectWithData(allSongsData!, options: NSJSONReadingOptions(0), error: nil)
if let json = allSongs as? Array<AnyObject> {
for index in 0...json.count-1 {
let songID : AnyObject? = json[index]
let collection = songID! as Dictionary<String, AnyObject>
let ID : AnyObject! = collection["ID"]
self.partyID.append(ID as String)
}
}
So essentially, how can I utilize a POST variable in my PHP script and then parse JSON based on the returned results?