我正在开发一个在后台工作的应用程序,它可以获取用户的位置并使用http请求将其发送到服务器。我的第一个目的是每隔n分钟获得用户的位置,但是经过大量的研究和试验,我放弃了,因为ios在3分钟后就杀死了我的后台任务。
然后我尝试在MonitoringSignificantLocationChanges上工作,但是由于手机塔的使用,它的不准确的位置更新破坏了我的应用程序的用途。
我们非常赞赏对下列任何一项的解决办法:
发布于 2015-10-25 03:17:48
这就是我要做的,我使用CLLocationManagerDelegate,寄存器在didUpdateLocations上和应用程序中进行更新。
- (void)applicationDidBecomeActive:(UIApplication *)application {
[_locationManager stopMonitoringSignificantLocationChanges];
[_locationManager startUpdatingLocation];
}
我开始更新位置,对我来说,关键是当应用程序转到后台时,我切换到重要的位置更改,这样应用程序就不会像这样耗尽面糊:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[_locationManager startMonitoringSignificantLocationChanges];
}
在didUpdateLocations中,您可以检查
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
isInBackground = YES;
}
并在后台启动一个任务来报告位置,例如
if (isInBackground) {
[self sendBackgroundLocationToServer:self.location];
}
开始一项任务,我希望这会有所帮助。
发布于 2015-06-19 14:33:19
在SignificantLocationChanges上高精度地获取用户的背景定位(用gps)
做以下工作:
在info.plist中添加以下内容
<key>NSLocationAlwaysUsageDescription</key>
<string>{your app name} requests your location coordinates.</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
在代码中,使用LoctionManager获取位置更新,(它将在前景和背景中工作)
@interface MyViewController <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation MyViewController
-(void)startLocationUpdates {
// Create the location manager if this object does not
// already have one.
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.activityType = CLActivityTypeFitness;
// Movement threshold for new events.
self.locationManager.distanceFilter = 25; // meters
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
}
- (void)stopLocationUpdates {
[self.locationManager stopUpdatingLocation];
}
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// Add your logic here
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"%@", error);
}
https://stackoverflow.com/questions/30940451
复制相似问题