简体   繁体   中英

Send array of Query strings from iOS to PHP

So, basically I have an array with INSERT queries which were generated in the iOS app, and I need to send them to a PHP Script for processing.

I needed help as to how to set up the POST request on the iOS side, to send the array to PHP.

Any help would be appreciated, thanks!

you can use AFNetworking ( https://github.com/AFNetworking/AFNetworking ) to make the post request.

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://samwize.com/"]];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                        path:@"http://samwize.com/api/pig/"
                                                  parameters:@{@"name":@"piggy"}];

Code attribution and tutorial: http://samwize.com/2012/10/25/simple-get-post-afnetworking/

And you can see a very nice tutorial here: http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

For this one possible solution is to just make a string which contains all the items of array separated by a special character. Now at server side, you can get this string and make array their by breaking string with the special character used.

Example:

NSArray *arr = [[NSArray alloc] initWithObjects:@"item 1",@"item 2",@"item 3",@"item 4", nil];
NSMutableString *testStr = [[NSMutableString alloc] init];
for (int i = 0; i < [arr count]; i++) {
    if (![testStr length]) {
        [testStr appendString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:i]]];
    } else {
        [testStr appendString:[NSString stringWithFormat:@"*%@",[arr objectAtIndex:i]]];
    }


}
NSLog(@"%@",testStr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM