2015-05-17 6 views
15

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

  1. Dodano rdzeń Lokalizacja ramowa pod docelowa ->Ogólne ->Linked Frameworks & biblioteki
  2. 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 
    
  3. 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

plist image

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ń.

+0

Wartości tego opisu użycia są puste. Jesteś pewien, że je wprowadziłeś? Dlaczego masz dwa opisy użycia? –

+0

@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

+0

Spróbuj usunąć blok if 'if (kod == kCLAuthorizationStatusNotDetermined && ....' i po prostu wywołaj 'requestAlwaysAuthorization' lub' requestWhenInUseAuthorization' na instancji 'locationManager' –

Odpowiedz

6

Tak! Mam rozwiązanie, Oto mój cały kod & rzeczy dodane, aby działało. Specjalne podziękowania dla @MBarton za jego wspaniałą pomoc. Również dzięki @ Vinh Nguyen za poświęcenie swojego cennego czasu na rozwiązanie mojego problemu.

Dodany Rdzeń Lokalizacja ramowa pod Target-> General-> Linked Frameworks & Biblioteki

Dodane w .plist pliku

NSLocationAlwaysUsageDescription 

patrz obrazek:

plist screenshot

w moim ViewController.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import <MapKit/MKAnnotation.h> 

// #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 

@interface ViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> 
{ 
    __weak IBOutlet UINavigationItem *navigationItem; 
} 

@property (weak, nonatomic) IBOutlet MKMapView *mapView; 
@property(nonatomic, retain) CLLocationManager *locationManager; 

@end 

Następnie w ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize mapView; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    [self setUpMap]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)setUpMap 
{ 
    mapView.delegate = self; 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
#ifdef __IPHONE_8_0 
    // if(IS_OS_8_OR_LATER) { 
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
     // Use one or the other, not both. Depending on what you put in info.plist 
     [self.locationManager requestAlwaysAuthorization]; 
    } 
#endif 
    [self.locationManager startUpdatingLocation]; 

    mapView.showsUserLocation = YES; 
    [mapView setMapType:MKMapTypeStandard]; 
    [mapView setZoomEnabled:YES]; 
    [mapView setScrollEnabled:YES]; 
} 

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:YES]; 

    self.locationManager.distanceFilter = kCLDistanceFilterNone; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [self.locationManager startUpdatingLocation]; 
    NSLog(@"%@", [self deviceLocation]); 

    //View Area 
    MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } }; 
    region.center.latitude = self.locationManager.location.coordinate.latitude; 
    region.center.longitude = self.locationManager.location.coordinate.longitude; 
    region.span.longitudeDelta = 0.005f; 
    region.span.longitudeDelta = 0.005f; 
    [mapView setRegion:region animated:YES]; 

} 

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800); 
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; 
} 
- (NSString *)deviceLocation { 
    return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude]; 
} 

... Ufff! Dostałem rozwiązanie po walce z wieloma kodami od ostatnich 5 dni ...

+0

Nie używaj 'IS_OS_8_OR_LATER'. Istnieją odpowiednie sposoby sprawdzenia, czy interfejs API jest dostępny, czy nie. – rmaddy

+0

@rmaddy Zaktualizowano mój kod, uprzejmie przejrzyj i przekaż swoje cenne sugestie. Dziękuję za poinformowanie mnie o tym. – iYoung

+0

To lepiej. Całkowicie usunę z odpowiedzi wszystkie odniesienia do tych makr. – rmaddy

Powiązane problemy