ios - 在 iOS 8 中获取 CoreLocation

标签 ios iphone ios8

我正在尝试通过我的应用获取当前用户位置。我编写了所有委托(delegate)方法,在 info.plist 中添加了字符串,但它仍然没有调用委托(delegate)方法。请帮助我。

 - (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    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) {
        longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
}

Screenshot of added keys in my app info.plist Error finding keys

最佳答案

控制台日志本身说您缺少在 info.plist 文件中添加适当的 key 你应该添加

NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription

带有相关描述的 key ,然后请求用户允许访问。

link可能会有帮助。

因为您已经添加了这些 key ,请尝试以下操作

  1. 删除 key 并重新添加。
  2. 完成后尝试清理然后构建您的应用程序。

关于ios - 在 iOS 8 中获取 CoreLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29901101/

相关文章:

ios - 如何检查核心数据中是否存在对象

iphone - NSTimer在单独的线程上,因此它不会占用UI元素

iPhone - 快速应用程序切换和 iOS 4

swift : type String doesn't conform to protocol AnyObject

c++ - 尝试链接 libFlurryAds 时链接器错误 Duplicate Symbol

ios - 继续引用

iphone - 如何向 UILabel 添加滚动功能

iphone - "SHK.h"找不到文件

ios - 从照片中提取 GPS 数据

objective-c - iOS8 Xcode6 UICollectionView sizeForIndexPath : does not get called on rotation