I am using NSMutableURLRequest to get data from my server. The Codeigniter based api endpoint that I'm fetching from works 100% of the time from a browser for the website, but works roughly 75% of the time for the app.
I ran a simple test where I send post values to the server and spit them back to the app. I'm sending the same values every time and I only receive the correct response roughly 75% of the time.
Here is my app code.
// Request
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", URL_API, endpoint]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setHTTPMethod:method];
[request setValue:contentType forHTTPHeaderField:@"content-type"];
[request setTimeoutInterval:timeoutInterval];
DLog(@"bodyValues: %@", bodyValues);
// Set body
[request setHTTPBody:[self encodeDictionary:bodyValues]];
// Connection
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
data = [[NSMutableData alloc] init];
}
else {
[self showError];
}
Here is my server's code.
header('HTTP/1.1 200 OK');
header('Status: 200 OK');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Request-Method: GET,POST');
header('Access-Control-Allow-Headers: x-requested-with');
header('Content-Type: application/x-json;charset=utf-8');
header('Connection: keep-alive');
print(json_encode($this->input->post()));
If there is any other code that I can provide just let me know. Thank you in advance!
Edit: Also, this is a problem on both the simulator and the device.