我正在尝试获取位置更新,尤其是在应用程序处于后台时。当应用程序进入后台时,应每隔n分钟发送一次位置更新
首先,我尝试使用前景,它工作正常。我将相同的代码复制到applicationDidEnterBackground,但无法打印位置更新。我不知道我被打到哪里,请帮助我。
在viewDidLoad中
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
和
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
latLabel.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
longLabel.text = longt;
}
我尝试将这些方法粘贴到AppDelegate中。它不起作用:(。帮助我
最佳答案
您不能简单地将代码粘贴到那里,并期望您的应用程序在后台运行。您必须将其注册为在后台运行的应用程序。请参阅this article中的实施长期运行的后台任务部分
关于iphone - 仅在应用程序处于后台时获取位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10136113/