【问题标题】:Not able to get the user's location in iOS 8 using CLLocation Manager?无法使用 CLLocation Manager 在 iOS 8 中获取用户的位置?
【发布时间】:2014-12-10 03:39:38
【问题描述】:

我尝试获取用户的纬度和经度,但结果为 0.000000 - 我尝试了以下代码。

  • 我已经分配了位置管理器变量。
  • Lo​​cation Manager 已分配并将委托设置为 self。
  • 检查了 iOS 8 版本验证请求。
  • 使用位置开始更新位置功能。

代码:

locationmanager=[[CLLocationManager alloc]init];
locationmanager.delegate=self;
// check before requesting, otherwise it might crash in older version
if ([locationmanager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

    [locationmanager requestWhenInUseAuthorization];

}
[locationmanager startUpdatingLocation];

#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"locations %@",locations);
    CLLocation*location = [locations lastObject];
    NSLog(@"location %f",location.coordinate.latitude);
}   

结果:2014-10-15 11:10:20.118 好[714:25676] 纬度 0.000000

【问题讨论】:

  • 请编辑您的帖子以使其可读...

标签: objective-c dictionary ios8 core-location cllocationmanager


【解决方案1】:

在 iOS 8 中,此代码会静默失败,即您不会收到任何错误或警告。

你需要做两件额外的事情才能让定位工作:

  1. 向您的 Info.plist 添加密钥
  2. 请求授权,要求位置管理器开始。

以下任何一个或两个键都需要添加到 Info.plist 文件中。

  1. NSLocationWhenInUseUsageDescription
  2. NSLocationAlwaysUsageDescription

这些是字符串类型,值可以是任何消息描述或空。

现在您需要为相应的定位方法请求授权。使用以下任一调用:

  1. [self.locationManager requestWhenInUseAuthorization]
  2. [self.locationManager requestAlwaysAuthorization]

【讨论】:

    【解决方案2】:

    #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) - (void)viewDidLoad {

    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    
    self.locationManager.delegate = self;
    if(IS_OS_8_OR_LATER){
        NSUInteger code = [CLLocationManager authorizationStatus];
        if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
            // choose one request according to your business.
            if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
                [self.locationManager requestAlwaysAuthorization];
            } else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
                [self.locationManager  requestWhenInUseAuthorization];
            } else {
                NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
            }
        }
    }
    [self.locationManager startUpdatingLocation];
    

    }

    #pragma 标记 - CLLocationManagerDelegate

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"didFailWithError: %@", error);
        UIAlertView *errorAlert = [[UIAlertView alloc]
                                   initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        NSLog(@"didUpdateToLocation: %@", newLocation);
        CLLocation *currentLocation = newLocation;
    
        if (currentLocation != nil) {
            longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
            latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
        }
    }
    

    在 iOS 8 中,您需要做两件额外的事情才能让定位工作:添加 您的 Info.plist 的密钥并从该位置请求授权 经理要求它开始。新的有两个 Info.plist 键 位置授权。需要这些密钥中的一个或两个。如果 两个键都不存在,您可以调用 startUpdatingLocation 但 位置管理器实际上不会启动。它不会发送失败 给委托人的消息(因为它从未开始,它不能 失败)。如果您添加一个或两个键但忘记了,它也会失败 明确请求授权。所以你需要做的第一件事 是将以下一个或两个键添加到您的 Info.plist 文件中:

    • NSLocationWhenInUseUsageDescription
    • NSLocationAlwaysUsageDescription

    这两个键都有一个字符串

    这是对您需要定位服务的原因的描述。你可以 输入一个字符串,例如“需要位置才能找到您的位置” 与 iOS 7 一样,可以在 InfoPlist.strings 文件中进行本地化。

    【讨论】:

      【解决方案3】:

      确保您在 .plist 文件中添加了以下行 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription。

      【讨论】:

      • 由于某种原因,NSLocationWhenInUseUsageDescription 默认以BOOL 添加到我的 plist 中。我的应用程序中的定位服务按预期工作,但选择 Settings->MyApp->Privacy->Location Services 导致 Settings.app 崩溃。将 NSLocationWhenInUseUsageDescription 的值类型更改为字符串(即使没有实际值)修复了崩溃。
      猜你喜欢
      • 2015-03-05
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多