2016-07-27 9 views
0

Ich entwickle eine Anwendung in Xcode 7.3 für iPhone 9.3.3 in Objective-C. Ich folgte den Vorschlägen auf dieser Seite - http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/, um den Standort des iPhone zu erhalten. Ich teste was ich bisher mit einem iPhone 9.3.3 über Blitzkabel habe.Core Location funktioniert nicht in Xcode 7.3

Ich habe "NSLocationWhenInUseUsageDescription" zum Plist mit einem Wert von "Request Location" hinzugefügt.

Ich bin verwirrt, warum das nicht funktioniert. Ich habe keine Probleme mit dem Standort über NSLog.

Jede Hilfe wird sehr geschätzt.


ViewController.h

#import <UIKit/UIKit.h> 

#import <CoreLocation/CoreLocation.h> 

#import <MapKit/MapKit.h> 


@interface ViewController : UIViewController<CLLocationManagerDelegate> 


@property (strong, nonatomic) CLLocationManager *locationManager; 

@property (weak, nonatomic) CLLocationManager *locations; 


// 

//@property (weak, nonatomic) IBOutlet UILabel *longitudelabel; 

//@property (weak, nonatomic) IBOutlet UILabel *latitudelabel; 


@end 

ViewController.m

#import <UIKit/UIKit.h> 

#import <Foundation/Foundation.h> 

#import "ViewController.h" 





@interface ViewController() 

{ 

    NSUserDefaults *defaults; 

} 



@end 



@implementation PollenViewController 



- (void)viewDidLoad 

{ 

    [super viewDidLoad]; 



    // ** Don't forget to add NSLocationWhenInUseUsageDescription in MyApp-Info.plist and give it a string 



    self.locationManager = [[CLLocationManager alloc] init]; 

    self.locationManager.delegate = self; 

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7. 

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 

     [self.locationManager requestWhenInUseAuthorization]; 

    } 

    [self.locationManager startUpdatingLocation]; 



} 


-(void)viewWillAppear:(BOOL)animated 

{ 

    [super viewWillAppear:animated]; 


} 


// Location Manager Delegate Methods 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 

{ 

    NSLog(@"%@", [locations lastObject]); 

} 

@end 
+0

versagt, was mit ur-Code ist falsch? ist die Lage null? –

+0

Sie werden Ihren Code debuggen müssen. Überprüfen Sie den Wert von authorizationStatus vor und nach dem Aufruf von requestWhenInUseAuthorization. Implementieren Sie auch die Delegate-Methode 'locationManager: didChangeAuthorizationStatus:', um die Änderung des Autorisierungsstatus zu sehen. –

+0

@DuncanC Ich änderte meine .m zum folgenden - [link] (http: //i.imgur.com/m3quLKk.png), und trat mit dem Debugger durch. Wenn [self.locationManager requestWhenInUseAuthorization]; heißt nichts passiert in der App. Ich habe auch [Manager requestWhenInUseAuthorization] versucht und nichts bekommen. Irgendwelche Hinweise, wie das zu beheben ist? – Aranelmalina

Antwort

0

Mit iOS 8, eine Erlaubnis eingeholt und Anfangsort Dienste zu nutzen sind jetzt separate Aktionen. Da

requestWhenInUseAuthorization 

und

requestAlwaysAuthorization 

Methoden asynchron ausgeführt wird, kann die App nicht die Ortungsdienste sofort starten. Stattdessen müssen Sie die Delegate-Methode wie folgt implementieren, die zu einem beliebigen Zeitpunkt ausgelöst wird, zu dem sich der Autorisierungsstatus basierend auf der Benutzereingabe ändert.

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { 

    if (status == kCLAuthorizationStatusAuthorizedAlways 
     || status == kCLAuthorizationStatusAuthorizedWhenInUse) { 
     [manager startUpdatingLocation]; 
    } 
} 

Wenn die Benutzer zuvor die Erlaubnis Dienste zu nutzen Ort gegeben hat, wird auch die delegierte Methode aufgerufen werden, nachdem der Standort-Manager initialisiert und hat seine Delegaten mit dem entsprechenden Berechtigungsstatus gesetzt, wie

CLLocationManager *manager = [CLLocationManager new] 
Manager.delegate = self; 
if [CLLocationManager locationServicesEnabled] { 
    [manager startUpdatingLocation]; 
} 
0
folgt

Ich konnte den Standort abrufen und in der App mit dem folgenden Code für ViewController.m anzeigen. @ DuncanC's Kommentar half sehr. Dies beinhaltet auch die umgekehrte Geokodierung.

- NSLocationWhenInUseUsageDescription in der Info.plists mit String "Need Location" verwendet. - Stellen Sie sicher, dass Einstellungen-> Datenschutz-> Standortdienste-> AppName auf "Während der Verwendung" und nicht "Nie" eingestellt war.

Ich hoffe, dies ist hilfreich für jeden, der feststeckt.

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import "CoreLocation/CoreLocation.h" 

@interface ViewController() 
{ 
    NSUserDefaults *defaults; 

    NSString *locationfound; 


} 

@property (strong, nonatomic) CLLocationManager *locationManager; 

@end 

@implementation PollenViewController { 
    CLGeocoder *geocoder; 
    CLPlacemark *placemark; 
    } 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //implement location finder 
    self.locationManager=[[CLLocationManager alloc] init]; 
    self.locationManager.delegate=self; 
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest; 

    //initialize geocoder for later 
    geocoder = [[CLGeocoder alloc] init]; 


} 

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


} 
//CURRENTLY WORKING LOCATION MANAGER 
//roughly based on ---/*https://gist.github.com/paulw11/84b18044942e2ceea52a8*/ 

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus: 
(CLAuthorizationStatus)status { 
    defaults = [NSUserDefaults standardUserDefaults]; 

    NSLog(@"Callback"); 
    NSLog(@"status %i",status); 
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) { 
     NSLog(@"Authorized"); 
     [self.locationManager startUpdatingLocation]; 

    } else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) { 
     NSLog(@"Denied"); 
     locationfound=[NSString stringWithFormat:@"No Location Found"];; 
     self.LocationLabel.text=locationfound; 

    }else{ 
     [self.locationManager requestWhenInUseAuthorization]; 
     [self.locationManager requestAlwaysAuthorization]; 
    } 
} 

// Location Manager Delegate Methods 
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *newLocation = [locations lastObject]; 

    NSLog(@"Location new %@",newLocation) ; 

    // Reverse Geocoding 
    NSLog(@"Resolving the Address"); 
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     defaults = [NSUserDefaults standardUserDefaults]; 

//  NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 

     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 
      locationfound= [NSString stringWithFormat:@"%@, %@", 
           placemark.locality, 
           placemark.administrativeArea]; 
      NSLog(@"%@",locationfound); 
      self.LocationLabel.text=locationfound; 
     } else { 
      NSLog(@"%@", error.debugDescription); 
      locationfound=[NSString stringWithFormat:@"No Location Found"];; 
      self.LocationLabel.text=locationfound; 
     } 

    } ]; 
} 

@end 
0

könnten Sie

versuchen
- (void)locationManager:(CLLocationManager*) manager didFailWithError:(NSError*)error 

Um zu sehen, ob das Update und warum