【问题标题】:how to get current location lat long in ios 8?如何在 ios 8 中获取当前位置 lat long?
【发布时间】:2014-10-01 05:25:15
【问题描述】:

我的问题是没有在 ios 8 中获取当前位置的纬度和经度。我尝试在 .plist 文件中为 ios 8 设置密钥,但没有调用此方法 - didUpdateToLocation:请帮助解决这个问题。

我的代码是:-

- (void)viewDidLoad

{

[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

if(IS_OS_8_OR_LATER) {
    //[self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startUpdatingLocation];
}

}

【问题讨论】:

  • iOS 是否要求您提供位置权限?如果您在模拟器中进行测试 - 您是否设置了测试位置?
  • 仅供参考 - 不要检查 iOS 版本。检查是否存在所需的方法。
  • 是的,我在模拟器中测试并在 .plist 文件 NSLocationWhenInUseUsageDescription 和 NSLocationAlwaysUsageDescription 中设置这两个键
  • 看到这个链接rshankar.com/get-your-current-address-in-swift虽然它在迅速但你可以检查权限等。

标签: objective-c ios8


【解决方案1】:
#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 mark - 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 文件中进行本地化。

【讨论】:

  • 不要使用IS_OS_8_OR_LATER。有适当的方法来检查 API 是否可用。
  • @TheCodingArt 根本不需要检查版本。正确的方法是使用 respondsToSelector: 之类的东西检查 API。
  • @rmaddy... 这实际上是一种非常糟糕的方法。到目前为止,这不是一个正确的方法(尤其是在当今使用 Swift 的时代)。 respondsToSelector 具有忽略私有和公共实现以及其他事物的共同假设。您绝对应该检查版本而不是依赖它。
  • 不管采用哪种方法,我都指出,如果您需要检查版本,添加自己的自定义宏不仅不可靠(因为 systemVersion 并不总是反映正确的 iOS 子版本) ,但也是多余的。
猜你喜欢
  • 1970-01-01
  • 2012-03-20
  • 2013-09-24
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多