当我调用API和锁定移动屏幕或最小化应用程序时,Alamofire会给我一个错误。
“操作无法完成。软件导致连接中止”
请给我一个解决这个问题的方法。
我认为iOS 12在应用进入后台时,在最后一个请求返回结果之前关闭连接。您可以使用以下代码来解决您的问题:
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
[self sendBackgroundDataToServer];
}
- (void) sendBackgroundDataToServer {
UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:2];
[dictionary setObject:[NSNumber numberWithDouble:lc.coordinate.latitude] forKey:@"floLatitude"];
[dictionary setObject:[NSNumber numberWithDouble:lc.coordinate.longitude] forKey:@"floLongitude"];
// send to server with a synchronous request
// AFTER ALL THE UPDATES, close the task
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}
}