我有一个应用程序,在后台运行时向服务器发送数据。这是负责数据发送的代码:
-(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;
}
因此,inputData
是一个由字符串组成的简单字典。
doRequest
方法基于NSURLSession,并且基本上看起来像这样:
-(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];
}
就像我之前提到的,所有东西都通过Wi-Fi运行,应用进入后台几分钟后,一个自定义蓝牙设备发送一些数据并将应用程序从挂起模式中唤醒。如果设备连接在3G上,则iOS应用程序在接收到数据后无法将其发送到服务器。我确定通过蓝牙发送的数据已被接收,因为它存储在本地数据库中。
还有一个重要的事实。如果通过Xcode运行应用程序,即使设备连接在3G上,该应用程序也会从后台发送数据。为了做到这一点,我先运行应用程序,然后点击Home按钮将其放入后台。
不知道为什么当应用程序通过电缆连接到Mac时,它会表现得不同,为什么数据无法通过3G(甚至2G)发送?
附加信息:
我不是尝试上传文件,而只是向服务器发送JSON。