我正在开发一个IOS应用程序,它每隔30秒将用户位置发送到后端服务器,即使在后台也是如此。如果我在后台设置它(在iphone上按下一次圆形按钮),它会成功发送位置,但是当我强制关闭它时(按下圆形iphone按钮两次,并强制关闭应用程序),位置停止已发送.i从我必须先登录的位置请求中获取错误。当我按下应用程序图标时,应用程序正常打开而没有任何登录。 我的代码如下:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self.locationManager stopUpdatingLocation];
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
[NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
}
和请求:
- (void)sendLocations{
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSString *statId = [[NSUserDefaults standardUserDefaults]objectForKey:@"statusId"];
if(!(statId == nil)){
[Requests updateUserLocation:(longitude) statusId:(statId) latitude:(latitude) block:^(BOOL success, NSError *error) {
if(success){
NSLog(@"Location Sucessfully sent");
}
else{
NSLog(@"location Not Success");
}
}];
}
else{
NSLog(@"NO Location");
}
}