I have a simple NSArray
in Xcode
which I'd like to pass to PHP file and use it as a loop. How can I do this?
For example, my NSArray looks like this:
NSArray *array = [NSArray arrayWithObjects: @"hello", @"hi", @"how are you", nil];
What I have done so far but no success
NSArray *array = [NSArray arrayWithObjects:@"hello", @"hi", @"how are you?", nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *postString = [NSString stringWithFormat:@"array=%@", array];
NSData* data = [postString dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
NSURL* url = [NSURL URLWithString:@"http://url.com/file.php"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:data];
__block NSString *resultString;
NSURLSessionDataTask* dataTask = [defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
resultString = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSLog(@"Data: %@",resultString);
}
}];
[dataTask resume];
My PHP. I went through a tutorial here
$array = array();
$array = $_POST['array'];
$arrlength = count($array);
for ($i=1; $i <= $arrlength; $i++)
{
//do something
}
I tried echoing the $array
but it was empty. I know somethings about Objective-c but I have very little knowledge about PHP. My code can $_POST NSStrings
but I'm still confused of how I can do this with NSArray
.
If anyone could please give me some pointers or give me links on some tutorials links that will be great. I have tried searching on stackoverflow and google on how to this, but their wasn't any thing I could understand.
So my question is really simple. How can I convert NSArray
to indexed arrays in PHP?