In my application, i made an activity to get input numbers from the user, the number of textfields for input data is different each time, it's a tableView and each cell has a textfield, i managed to store all the numbers in a NSMutableArray
, and I've been looking into how to pass it to my php, I can't seem to find something to help me, and it can't be send as NSString using "StringWithFormat
".
Should NSMutableArray
Be changed into NSDictionary
? How Do i Send it to my php code ?
I am using AFNetworking
to manage all my connections and it's great, just got stuck on sending an array.
each object in my NSMutableArray
consists of
StudentName, StudentMark, StudentID
any suggestions or good tutorials i can learn from ?
AFNetworking, by default, uses the
AFHTTPRequestSerializer
(which createsapplication/x-www-form-urlencoded
requests). This is not well suited for sending an array of values (esp an array of dictionaries).You have a couple of options, assuming you have an array of
students
:Manually use
NSJSONSerialization
to encode the array of dictionaries into a string, and then pass that as a parameter in a dictionary:If you do that, your server code would grab
$_POST['students']
, and thenjson_decode
that.Change the web service to accept JSON requests (i.e. an
application/json
request rather than anapplication/x-www-form-urlencoded
requests).If you do that, though, it requires a more fundamental change in your web service to accept JSON requests (e.g., rather than using
$_POST
, you'd manually grab the input and JSON decode it:The first option is probably easiest, but lacks a certain elegance (embedding JSON within a
x-www-form-urlencoded
request). The latter option (changing web service to accept JSON requests) would require a little more work, though (e.g. once you have one web service request using JSON, you might want the whole web service to use JSON consistently).I don't know anything about IOS and objective-c so I don't know how they send post data to a server. The platforms that I know about allow the sending of JSON and that is what would be used to send an array to PHP. PHP then simply json_decodes the data into a PHP array. So, if you use NSJSONSerialization (just looked that up) you can send your data and process it in PHP.