2015-10-18 171 views
0

我的代碼包含在下面。這是一個旨在返回當前位置座標的類。我得到的唯一輸出是「before before after after」 - 圍繞requestAlwaysAuthorization的打印行。然後該應用程序崩潰。請求對話框有時會短暫顯示,有時會顯示幾秒鐘。在很少的情況下,我甚至可以按「OK」。該應用程序總是崩潰。我在xCode 7.0中遇到了這個問題,現在在這兩種情況下都是xCode 7.0.1,iOS 9.0。我已經搜索了StackOverflow的高低,關於這個主題的大部分問題都是針對xCode和iOS的早期版本的,而且我的案例中沒有任何發佈的解決方案對我有幫助。因此,這個問題。我也發現了YouTube教程,這些教程基本上做了我正在做的事情,但沒有喜悅。我在我的plist中也有NSLocationAlwaysUsageDescription,並且我有隱私 - 位置使用描述和NSLocationWhenInUseUsageDescription用於很好的衡量。我也嘗試通過Product \ Scheme菜單從xCode發送位置,並且我也嘗試使用模擬器的Debug \ Location。我已經嘗試了幾種不同的位置選項。模擬器的地圖應用程序似乎總是工作。而蘋果的LocateMe(用Objective C編寫)也適用。 Swift 2(我的代碼如下)失敗。iOS模擬器崩潰requestAlwaysAuthorization()

import CoreLocation 

class TheCurrentLocation: NSObject, CLLocationManagerDelegate { 

    var locationManager: CLLocationManager! 
    var latitude: Double = 0 
    var longitude: Double = 0 
    var locationStatus: NSString = "Not Started" 

    func initialize() { 

     self.locationManager = CLLocationManager() 
     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer 
     print("before") 
     self.locationManager.requestAlwaysAuthorization() 
//  self.locationManager.requestWhenInUseAuthorization() 
     print("after") 

    } // END: initialize() 

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 

     self.locationManager.stopUpdatingLocation() 
     print("ERRORS: " + error.localizedDescription) 

    } // END: locationManager delegate didFailWithError 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     let location = locations.last 
     let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 
     self.locationManager.stopUpdatingLocation() 
     print ("ta da") 
     self.latitude = center.latitude 
     self.longitude = center.longitude 

    } // END: locationManager delegate didUpdateLocations 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 

     var isAllowed = false 

     switch status { 
     case CLAuthorizationStatus.Restricted: 
      locationStatus = "Restricted Access to Location" 
     case CLAuthorizationStatus.Denied: 
      locationStatus = "User Denied Access to Location" 
     case CLAuthorizationStatus.NotDetermined: 
      locationStatus = "Location Status Not Determined" 
     default: 
      locationStatus = "Allowed Access to Location" 
      isAllowed = true 
     } // END switch status 

     if (isAllowed == true) { 
      NSLog(String(locationStatus)) 
      self.locationManager.startUpdatingLocation() 
     } else { 
      NSLog(String(locationStatus)) 
     } 

    } // END: locationManager delegate didChangeAuthorizationStatus 

} // END: theCurrentLocation 
+2

更新您的問題與關於崩潰的細節。它在哪裏崩潰,什麼是完整和準確的錯誤信息? – rmaddy

+0

沒有錯誤信息。我希望我能告訴你更多。除已經描述的印刷線之外,沒有其他輸出。 –

+0

@RobWelan在Xcode中,當你的應用程序崩潰時,你可以通過在調試器的'(lldb)'提示符下鍵入'bt'來獲得一個報告崩潰錯誤的回溯。 – Daniel

回答

1

我想我知道是什麼原因造成的。你不能有NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription。只有其中一個。我想我之前遇到過這個問題。嘗試刪除其中一個,看看是否修復它。

+0

爲什麼要告訴用戶在他們的問題表明他們都有這兩個關鍵字時添加此密鑰。更新你的答案。 – rmaddy

+0

我已經這樣做了。 – mn1

+0

@ mn1:我已經刪除了我的plist中的WhenInUsage和隱私項目(請參閱我的原始問題帖子,瞭解每個項目的全名)。沒有幫助不幸。 –