【问题标题】:Why are location services not being used after I allow them?为什么我允许定位服务后没有使用?
【发布时间】:2014-09-04 23:53:45
【问题描述】:

在我的 iOS 应用程序中,我有一个表格,它使用您的位置来查找您附近的数据。我今天刚刚将它安装在我的 iPhone 上,当需要定位服务时,我收到了预期的警报,上面写着“MyAppName 想使用您当前的位置”,一旦我按下确定,应用程序就基本冻结了。但是,当我关闭应用程序并重新打开它时,它运行得非常好。现在我想起来了,这也发生在模拟器上,因为我必须运行该应用程序几次,然后位置服务才能在那里正常工作。其他人是否发生过这种情况,您知道如何解决吗?我不希望我的用户必须关闭应用并重新打开它才能使定位服务正常工作。

以下是一些相关代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"%@",error.userInfo);
    if([CLLocationManager locationServicesEnabled]){
        NSLog(@"Location Services Enabled");
        UIAlertView    *alert = [[UIAlertView alloc] initWithTitle:@"Need Your Location"
                                                           message:@"Please go to Settings and turn on Location Services so you can see who's near you."
                                                          delegate:nil
                                                 cancelButtonTitle:@"OK"
                                                 otherButtonTitles:nil];
        [alert show];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        _thisGeoPoint = [PFGeoPoint geoPointWithLatitude:currentLocation.coordinate.latitude
                                           longitude:currentLocation.coordinate.longitude];
    }
    [locationManager stopUpdatingLocation];
    located = true;
}

【问题讨论】:

  • 您需要在调试器中单步调试您的代码或添加日志记录以找出卡住的位置,但有几点 - didUpdateToLocation:fromLocation 方法已弃用,您应该使用 didUpdateLocations并且一旦获得位置就停止更新位置可能不是一个好主意,因为位置准确性会随着时间的推移而提高 - 第一次修复可能会非常不准确,具体取决于 GPS/位置服务的状态

标签: ios objective-c core-location cllocationmanager


【解决方案1】:

发生这种情况是因为 locationManager 在最初没有权限时停止运行。因此,一旦检测到权限已更改,您将需要重新运行它。您可以使用didChangeAuthorizationStatus delegate method 检查权限是否更改。

所以是这样的:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if (status == kCLAuthorizationStatusAuthorized) {
        [locationManager startUpdatingLocation];
    }
}

【讨论】:

  • 这不是我的经验。授予权限时调用didChangeAuthorizationStatus,但无需再次调用startUpdatingLocation
  • didChangeAuthorizationStatus 在显示警报时被调用,但在我按下“允许”时没有被调用,所以我得到的结果与以前相同。知道为什么会发生这种情况@Paulw11?
  • 没有。你运行的是什么版本的iOS?您是否尝试过从设备中删除您的应用并重新安装。我发现隐私设置可能会搞砸。
  • @Paulw11 我正在运行 iOS 7,并且每次都尝试删除并重新安装,因为问题仅在第一次使用时出现。有没有办法在调用 viewDidLoad 之前请求位置服务的许可?我认为这可能会解决问题。
  • 当你调用startUpdatingLocation时会显示权限对话框,所以你可以在你的appDelegate中调用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 2012-02-25
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
相关资源
最近更新 更多