2014-10-04 41 views
2

我的應用程序由Xcode的5 ios7,當我按下按鈕「獲取位置」出現消息「MyApp的想用你的當前位置」,現在我更新Xcode和創建運行在ios8核心位置不運行,缺少一些關鍵要添加?還是某個班?在ios7中沒有必要實現?核心位置並不在iOS模擬器

回答

3

的iOS 8 CoreLocation API已經改變。

所以,你需要做的第一件事就是添加下列鍵的一個或兩個的Info.plist文件:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

這兩個鍵的一個字符串這是您需要位置服務的原因的描述。你可以像「是必需的位置,找出你在哪裏」,正如iOS中7,可在InfoPlist.strings文件本地化輸入一個字符串。

接下來,需要請求授權用於對應的位置的方法,或WhenInUse背景。使用以下電話之一:

[self.locationManager requestWhenInUseAuthorization] 
[self.locationManager requestAlwaysAuthorization] 

下面的代碼適用於ios7和ios8。

if (_locationmanager == nil) { 
    _locationmanager = [[CLLocationManager alloc] init]; 
} 
_locationmanager.delegate = self; 
_locationmanager.distanceFilter = kCLDistanceFilterNone; 
if ([CLLocationManager locationServicesEnabled]) { // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7. 

if ([_locationmanager respondsToSelector:@selector(requestWhenInUseAuthorization)])   { 
    [_locationmanager requestWhenInUseAuthorization]; 
} 
[_locationmanager startUpdatingLocation]; 
+0

完美!!!!跑!!!!!非常感謝你 – 2014-10-06 14:30:42

+0

謝謝你夢幻般的回答:) – Andy 2015-03-23 20:21:04