如何获取iOS设备的当前位置?
似乎Apple已经对如何获取位置进行了更改,但我无法找到最新的帖子。我自己在下面回答了这个问题:
答案 0 :(得分:0)
我无法找到获取当前位置的最新解决方案,所以我就是这样做的。
我的来源是Apple关于位置跟踪和智能手表的示例代码PotLocCoreLocationwithiPhoneandAppleWatch
:https://github.com/robovm/apple-ios-samples/tree/master/PotLocCoreLocationwithiPhoneandAppleWatch
这是您在应用中需要做的事情。
请注意,步骤2-6中的所有内容都在此要点中: https://gist.github.com/JonMercer/75b3f45cda16ee5d1e0955b2492f66dd
capabilities
标签(它是general
标签旁边的标签,您可以在其中放入套装标识符)。然后启用Background Mode
并启用Location updates
。 编辑感谢@Rob:并将NSLocationWhenInUseUsageDescription
添加到您的plist。价值可以是你喜欢的任何东西在视图控制器中,扩展CLLocationManagerDelegate
class YourClass: UIViewController, CLLocationManagerDelegate {
实施这两项功能
/**
Increases that location count by the number of locations received by the
manager. Updates the batch count with the added locations.
*/
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//use the "locations" variable here
}
/// Log any errors to the console.
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error occured: \(error.localizedDescription).")
}
创建let manager = CLLocationManager()
变量
viewDidLoad()
点设置以下内容:manager.requestWhenInUseAuthorization()
,manager.allowsBackgroundLocationUpdates = true
和manager.startUpdatingLocation()
manager.stopUpdatingLocation()
和manager.allowsBackgroundLocationUpdates = false