I have an application which sends data to the server while app is in the background. Here is the code responsible for data sending:
-(bool) sendStats: (MCStatsSender*) val{
if(![self checkInternet]){ //Using Reachability here
return false;
}
NSDictionary *inputData = [NSDictionary dictionaryWithObjectsAndKeys:
self.propertyA.value, "key1",
val.data, "key2",
nil];
[myNetworkManager doRequest:[myRequestManager createWithStringAndDictionary:MY_URL Data:inputData handler:myHandler user:val]];
return true;
}
So the inputData
is a simple dictionary with strings.
A method doRequest
is based on a NSURLSession and basically looks like this:
-(void) doRequest: (MCRequest*) request{
[tasks addObject:request];
if(m_session == nil){
NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"key-%lu",reqid]];
m_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
}
NSURLSessionDataTask* task = [m_session dataTaskWithRequest:request.generatedRequest];
request.reqId = task.taskIdentifier;
[task resume];
}
As I said, everything works through Wi-Fi, the app goes into background, and after few minutes, a custom bluetooth device sends some data and wakes an application from a suspended mode. After the data has been received by iOS application, it fails to send it to a server if a device is connected over 3G. I am positive that data sent via bluetooth is received because it is stored in local database.
Also there is one other important fact. If an application is run through the Xcode, even if a device is connected over 3G, the application will send a data from background. To do this, I run an app, then tap a Home button to put it into background.
Don't know what is the difference and why app acts differently when it is attached with a cable to Mac, and why data is not sent via 3G (or even 2G) ?
Additional info:
I am not trying to upload a file, but rather just send a JSON to a server.