我正在尝试弄清楚如何在 XCODE 中使用适用于 iOS 应用程序的 SWIFT 获取某人的当前位置。
我正在关注本教程 here ,但由于某种原因我的代码无法工作。我认为这是因为我使用的是更新的 Xcode 版本控制。由于某种原因,我的委托(delegate)有不同的参数,我不确定为什么,但是当我构建程序时,它甚至没有询问我是否要允许位置服务。
这是我的代码
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.requestWhenInUseAuthorization();
self.locationManager.startUpdatingLocation();
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// GPS STUFF
// UPDATE LOCATION
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!) { (placemarks, ErrorType) -> Void in
if(ErrorType != nil)
{
print("Error: " + ErrorType!.localizedDescription);
return;
}
if(placemarks?.count > 0)
{
let pm = placemarks![0] ;
self.displayLocationInfo(pm);
}
}
}
// STOP UPDATING LOCATION
func displayLocationInfo(placemark: CLPlacemark)
{
self.locationManager.stopUpdatingLocation();
print(placemark.locality);
print(placemark.postalCode);
print(placemark.administrativeArea);
print(placemark.country);
}
此外,在 info.plist 中我还添加了 NSLocationWhenInUseUsageDescription。
任何建议都会很好
最佳答案
在您的 Info.plist
中,添加以下内容:
或者您可以在 Info.plist
中添加如下内容:
<key>NSLocationAlwaysUsageDescription</key>
<string>To get current location</string>
这将要求您允许定位服务。
希望这对您有帮助。
关于ios - 如何在 Swift iOS 编程中获取某人的当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36513939/