0

I want to send array of <Full name, Email> pairs to my php webservice. Should I use NSMutable array of dictionaries? What is the proper way?

Update:

I want to send in JSON.

3
  • There is no json encode function ? Commented Jun 11, 2013 at 11:19
  • I want to send array of pair of elements in JSON. Commented Jun 11, 2013 at 11:23
  • The first half of this answer illustrates how to send JSON from iOS to PHP web service. It's demonstrating a dictionary, but the basic technique is the same for an array of dictionaries. Commented Jun 11, 2013 at 11:49

3 Answers 3

1

You can just pass dictionary as parameters so that your php script grabs those parameters with $_POST variable.

I will post a sample code which uses AFNetworking library.

NSArray *keys = [NSArray arrayWithObjects: @"key1", @"key2", nil];
NSArray *values = [NSArray arrayWithObjects: @"value1", @"value2", nil];
NSDictionary *parameters = [NSDictionary dictionaryWithObjects: values Keys: keys];

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL[NSURL URLWithString:@"Your  server url"];
NSURLRequest *request = [client requestWithMethod:@"POST" path:@"path" parameters:parameters];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
[operation start];

AFNetworking class takes care of everything for you. Generally, it is better to enqueue these operations in a NSOperationQueue. Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

0

If it's going to be JSON formatted data use dictionaries. Look into AFNetworking library which handles all of your networking calls asynchronously for you.

Comments

0

Use this logic:

NSDictionary *dic = [NSDictionary dictionaryWithObject:yourDictionary/Array forKey:@"yourkeyForWebServiceFile"];
                SBJsonWriter *json=[[SBJsonWriter alloc] init];
                NSString *jsonStr = [json stringWithObject:dic];

now send this jsonStr in your PHP file as Post request. I have used SBJson library in this you can use others also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.