Próbuję uzyskać bieżącej lokalizacji użytkownika za pomocą Core Location Framework w Xcode 6.3.1, zrobiłem następujące rzeczy:Podstawowe metody Lokalizacja pełnomocnik nie uzyskiwanie nazywane w iOS 8.3 Xcode 6.3.1
- Dodano rdzeń Lokalizacja ramowa pod docelowa ->Ogólne ->Linked Frameworks & biblioteki
My ViewController.h plik jest, jak pokazano poniżej,
#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
Mój plik ViewController.m jest, jak pokazano poniżej,
- (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
ja również dodać następujące klucze w moim pliku Info.plist
- NSLokalizacjaWhenInUseUsageDescription
- NSLocationAlway sUsageDescription
Sprawdzone wszystko podane here, here, here, here, i wiele więcej lista
Więc jest ktoś o rozwiązanie tego problemu, prosimy o pomoc. Straciłem rozum szukając tego przez cały dzień.
Wartości tego opisu użycia są puste. Jesteś pewien, że je wprowadziłeś? Dlaczego masz dwa opisy użycia? –
@VinhNguyen Próbowałem również poprzez umieszczanie wartości w tym również, ale to nie ma żadnego wpływu, i przez utrzymywanie tylko NSLocationAlwaysUsageDescription wtedy również to nie działa dla mnie. Próbowałem, że zbyt ... – iYoung
Spróbuj usunąć blok if 'if (kod == kCLAuthorizationStatusNotDetermined && ....' i po prostu wywołaj 'requestAlwaysAuthorization' lub' requestWhenInUseAuthorization' na instancji 'locationManager' –