2014-10-09 141 views
0

我有很多的麻烦下面的代码工作之前,我更新到iOS和Xcode的6守则里面viewDidLoad中写到:CLLocation不会开始更新到iOS 8之后更新位置

self.locationManager.delegate = self; 
[self.locationManager requestWhenInUseAuthorization]; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[self.locationManager startUpdatingLocation]; 

在我的.h文件中,我的LocationManager作为属性,也宣告CLLocationManagerDelegate:

@property CLLocationManager *locationManager; 

我设置里面

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

但代码从未运行。

我失去了什么步骤?

编辑:请注意提示询问用户允许位置服务从未出现。我怀疑这是问题所在,但是在我将代理人宣布为自己后,我确实请求了服务。

还增加

<key>NSLocationWhenInUseUsageDescription</key> 
<string>Your message goes here</string> 

但对我来说还是

+0

[iOS 8:位置服务不工作]的可能的重复(http://stackoverflow.com/questions/24062509/ios-8-location-services-not-working) – 2014-10-09 14:10:54

回答

0

因此最终“的iOS 8”问题,我意识到我的问题是我没有分配和初始化我的locationManager

self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self; 
[self.locationManager requestWhenInUseAuthorization]; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) { 
     [self.locationManager startUpdatingLocation]; 
    }else{ 
     [self.locationManager requestWhenInUseAuthorization]; 
     [self.locationManager requestAlwaysAuthorization]; 
    } 
[self.locationManager startUpdatingLocation]; 
0

您需要NSLocationWhenInUseUsageDescription键添加到您的info.plist不起作用。 (从我的测试,它可以是一个空字符串,但密钥必须存在,但不知道Apple是否无法检查空字符串)

+0

我已在添加NSLocationWhenInUseUsageDescription 您的消息去这里,但它仍然无法正常工作。 – 2014-10-09 14:19:29

0

您需要使用requestAlwaysAuthorization(用于后台位置)或requestWhenInUseAuthorization(仅用于位置当前台)在开始位置更新之前需要调用CLLocationManager。

还需要在Info.plist中使用NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription项,并在提示中显示一条消息。在这个岗位

的更多信息:Location Services not working in iOS 8

1

,如果你之前用户同意使用定位服务器启动位置

iOS8上或更高版本的变化

- (xx)xxxx 
{ 
    self.locationManager.delegate = self; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) { 
     [self.locationManager startUpdatingLocation]; 
    }else{ 
     [self.locationManager requestWhenInUseAuthorization]; 
    }  
} 

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
{ 
    if([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined){ 
     [self.locationManager startUpdatingLocation]; 
    } 
}