【发布时间】:2015-07-29 01:57:44
【问题描述】:
我正在尝试使用 Xcode 6.3.1 中的核心位置框架获取用户的当前位置,我做了以下事情:
- 在Target-> General-> Linked Frameworks & Libraries下添加了Core Location Framework
-
我的ViewController.h文件如下图,
#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController<CLLocationManagerDelegate> @property (weak, nonatomic) IBOutlet UILabel *lblLatitude; @property (weak, nonatomic) IBOutlet UILabel *lblLongitude; @property (weak, nonatomic) IBOutlet UILabel *lblAddress; @property (strong, nonatomic) CLLocationManager *locationManager; @end -
我的ViewController.m文件如下图,
- (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)])) { 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) { lblLatitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; lblLongitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; } } @end
我还在 info.plist 文件中添加了以下键
- NSLocationWhenInUseUsageDescription
- NSLocationAlwaysUsageDescription
检查了here、here、here、here 和更多列表的所有内容
那么,有没有人有解决这个问题的方法,请帮忙。一整天都在寻找这个而失去理智。
【问题讨论】:
-
那些使用说明的值为空。你确定你把它们放进去了?还有为什么你有两个用法说明?
-
@VinhNguyen 我也尝试过将值放入其中,但这没有任何影响,并且只保留 NSLocationAlwaysUsageDescription 对我也不起作用。也试过了...
-
尝试删除 if 块
if (code == kCLAuthorizationStatusNotDetermined &&....并简单地在您的locationManager实例上调用requestAlwaysAuthorization或requestWhenInUseAuthorization -
试过了,但仍然没有调用,甚至没有得到提示消息。
-
您的
IS_OS_8_OR_LATER宏中有什么内容?尝试删除它。
标签: ios core-location ios8.3 xcode6.3.1