Xcode 6.3.1 で Core Location Framework を使用してユーザーの現在の場所を取得しようとしています。次のことを行いました。
- 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、およびその他のリストのすべてを確認しました
それで、この問題の解決策を持っている人はいますか、親切に助けてください。一日中これを探して頭がおかしくなりました。