【问题标题】:didUpdateLocations does not get called when app enters foreground, core location, ios应用程序进入前台、核心位置、ios 时不会调用 didUpdateLocations
【发布时间】:2014-04-15 16:34:21
【问题描述】:

我的目标是每当应用程序进入前台时,我都会获取位置以更新我的天气预报。我要做的是

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m

}
- (void)viewWillAppear:(BOOL)animated
{
    self.locationManager.delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
#pragma mark - Click to get weather
-(void)enterForeground {
    [self.locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
 NSLog@"didUpdateLocations";
}

但是,enterForeGround 确实会在我进入前台时被调用,但 didUpdateLocations 不会一直被调用。 我不知道我错过了位置服务的一些关键内容。如果您有任何想法,请给我建议。 谢谢

【问题讨论】:

    标签: objective-c ios5 core-location viewwillappear


    【解决方案1】:

    从不保证会调用 didUpdateLocations。你可以做的是当 viewDidLoad 被调用时,你可以请求最后一个位置。 self.locationManager.location。

    您现在可以检查该位置的时间戳和精度。如果数据太旧或无法满足您的需求,则调用 startUpdatingLocation,当您根据过滤器和准确性得到修复时,将调用 didUpdateLocations。

    【讨论】:

      【解决方案2】:

      首先,您需要在后台添加定位服务,方法是转到 Targets -> Capabilities 并在“Background Modes”下选中“Location Updates”。 其次,您需要在适当的地方开始位置更新 [self.locationManager startUpdatingLocation](对于您上面在 viewWillAppear 中提供的代码似乎是最佳选择)

      - (void)viewWillAppear:(BOOL)animated
      {
          self.locationManager.delegate = self;
         [self.locationManager startUpdatingLocation]
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
      }
      

      现在,当您的应用进入后台模式并重新进入前台时,应该调用 locationManager 委托方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-14
        • 1970-01-01
        • 1970-01-01
        • 2016-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多