iphone - 仅在应用程序处于后台时获取位置更新

标签 iphone ios xcode cllocationmanager

我正在尝试获取位置更新,尤其是在应用程序处于后台时。当应用程序进入后台时,应每隔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/

相关文章:

iphone - 如何在box2d中动态更改主体的组索引

ios - 基于接近度在后台对 iBeacon 进行测距

ios - 更新 "developer rejected"应用程序 : "No eligible applications were found"

iphone - 通过单击键盘按钮关闭屏幕键盘的事件

xcode - 哪种文件是有效的Xcode行为脚本?

iphone - 创建项目后添加SenTest测试目标

iphone - NSURLConnection、基本身份验证和 Cookie 问题

iphone - 延迟图像下载完成后更新 UITableViewCell

ios - 什么样的变换会给我想要的结果图像?

iphone - 停止网络事件指示器