2016-09-04 56 views
0

我正在學習swift中的用戶本地化。我試圖在控制檯上打印本地化(以後我會用它來獲取標籤上的信息,所以我想檢查它是否有效),但我不知道爲什麼它不打印任何內容。即使將轉換刪除到字符串,並且只是爲了打印任何內容,它仍然不起作用。請幫忙。CLLocationManager問題

是的,我加了NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

import UIKit 
import CoreLocation 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate { 

    var locationManager = CLLocationManager() 
    var myPosition = CLLocationCoordinate2D() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     locationManager.delegate = self 
     locationManager.requestWhenInUseAuthorization() 
     locationManager.startUpdatingLocation() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) { 

     print("Got location: \(newLocation.coordinate.latitude), \(newLocation.coordinate.longitude)") 

     myPosition = newLocation.coordinate 

     locationManager.stopUpdatingLocation() 
    } 

} 
+0

您是否仔細檢查了應用設置應用中的「位置設置」?也許當你第一次運行它時,你意外地轉向了這個應用程序的位置設置。檢查'authorizationStatus',看看它返回什麼。此外,請確保您在物理設備上進行測試,因爲模擬器的行爲與物理設備不同。 – Rob

+0

沒有幫助...... – belab

+0

另外,術語「本地化」通常是指國際化(語言,數字/日期格式化程序等)。你問的是「位置服務」,而不是「本地化」。 – Rob

回答

1

didUpdateToLocationCLLocationManagerDelegate的一個過時的方法。它在10.6及更早版本中可用。相反,使用didUpdateLocations,它將按照最近時間的順序返回所有位置對象的數組。然後訪問最新的最新位置,即抓取返回數組中的最後一個對象。

那麼試試這個

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

      var latestLocation: CLLocation = locations.last; 
     print("Got location: \(latestLocation.coordinate.latitude), \(latestLocation.coordinate.longitude)") 

    } 

告訴我是怎麼回事。

+0

什麼都沒有。看起來這個func從來不會被調用。不知道爲什麼。 沒有其他錯誤,不會崩潰。 – belab

+0

在函數的第一行得到一個斷點,看看它是否也被稱爲 –

+0

,現在,擺脫stopUpdatingLocation - 並確保您按照我的答案任何一種方式,因爲它將是一個進步!你是如何將NSLocationAlwaysUsageDescription添加到plist文件的? –

相關問題