I have an application that sends data to the server while the application is in the background. Here is the code responsible for sending the data:
-(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 inputData is a simple dictionary with strings.
The doRequest method doRequest based on 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 via Wi-Fi , the application goes into the background, and after a few minutes the bluetooth user device sends some data and wakes the application from paused mode. After the data has been received by the iOS application, it cannot send it to the server if the device is connected via 3G. I am sure that the data sent via bluetooth has been received since it is stored in a local database.
There is also another important fact. If the application is launched via Xcode, even if the device is connected via 3G, the application will send data from the background. To do this, I launched the application, and then click the "Home" button to place it in the background.
I donβt know what the difference is and why the application works differently when it is connected via cable to the Mac, and why data is not sent via 3G (or even 2G)?
Additional Information:
I am not trying to upload the file, but just send JSON to the server.