2014-11-21 42 views
1

我正在測試CoreLocation以瞭解如何捕獲和記錄用戶的位置。我構建了一個簡單的Master-Detail應用程序,在詳細信息窗格中顯示用戶的現場位置。一切按預期工作。CoreLocation授權問題 - 從未提示授權

接下來,我開始製作我的真正的應用程序,它也使用CoreLocation。我使用Master-Detail風格構建了應用程序,並且使用戶打開詳細信息窗格時應顯示其當前位置,活動位置。然而,一切都沒有發生。

我調試和研究了很多後確定那是什麼,一旦被創建我的locationManager,我叫locationManager.startUpdatingLocation()的授權得到位置改變到denied(或假,或任何其所謂的)。我可以實時跟蹤的唯一方法是在Xcode中構建並運行應用程序,一旦它在模擬器中打開,打開位置服務並更改應用程序設置以允許位置跟蹤爲「始終」。然後我可以在控制檯看到正在獲取位置。

我不明白爲什麼我必須不斷地將授權更改爲「總是」一遍又一遍。我已經刪除了模擬器上的應用程序,從XCode完成了一個「乾淨的」以重新開始,並且仍然在未經授權的情況下在模擬器上啓動。

任何人有想法嗎?

UPDATE現在我看到,當我構建並運行在模擬器中時,我得到一個位置,然後授權被更改爲不允許位置服務。

這裏是我的代碼(在另一個應用程序的工作):

import UIKit 
import CoreLocation 

class DetailViewController: UIViewController, CLLocationManagerDelegate { 

var locationManager : CLLocationManager! 



func startTrackingLocation() { 
    println("tracking engaged") 
    locationManager = CLLocationManager() 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.requestAlwaysAuthorization() 
    locationManager.requestWhenInUseAuthorization() // <-- this was originally commented out 
    locationManager.startUpdatingLocation() 
    println("and we're tracking") 
    println(locationManager.location) 
} 

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
    println("location acquired") // <--- Only see this when I manually allow location when app is running 
    self.newLongLabel?.text = "\(locations[0].coordinate.longitude)" 
    self.newLatLabel?.text = "\(locations[0].coordinate.latitude)" 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.startTrackingLocation() 
    self.configureView() 
} 

// more class stuff... 
+0

沒有爲既要求同時,選擇一個:'requestAlwaysAuthorization'或'requestWhenInUseAuthorization'(我認爲你已經有兩個鍵在plist中) – TonyMkenu 2014-11-21 22:59:28

+0

@TonyMkenu - 我刪除了'requestAlwaysAuthorization',它沒有改變這種情況。 :/ – Garfonzo 2014-11-21 23:22:17

+0

你確定,你有plist的鑰匙嗎? ' NSLocationWhenInUseUsageDescription'或永遠? – TonyMkenu 2014-11-22 00:11:20

回答

0

如果您確定您所添加的關鍵plist文件嘗試在didChangeAuthorizationStatus檢查它:位置管理的委託方法。

要確保你的plist爲String類型有兩個鍵,並與值,您想向用戶顯示:

NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription

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

    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways){ 

     if ([CLLocationManager locationServicesEnabled]) { 
      [yourLocationManager startMonitoringSignificantLocationChanges]; 
      //run your code here 
     } 
    } 
}