2014-10-09 85 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]; 
    } 
}