我在我的应用中使用谷歌地图。我已在info.plist中设置了这个
隐私 - 使用时的位置用法说明
在我的代码(HomeScreen)中我也是这样检查的:
if (CLLocationManager.locationServicesEnabled())
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
} else{
let alertController = UIAlertController(title: "Oops !!" , message: "Location service seems to be disabled. Please enable from Settings -> Privacy ->LocationService.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
但是第一次安装应用时它没有请求权限。任何帮助都会有所帮助。
答案 0 :(得分:2)
您正在设置Info.plist
密钥,以便在用户使用应用程序时访问该位置(即,当它位于前台时),但在您的代码中,您在应用程序运行时请求权限(即总是)。
你需要决定你想要的。如果您希望始终能够访问用户的位置,请更改Info.plist
密钥。如果您想在应用程序位于前台时访问用户的位置,请将权限请求更改为requestWhenInUseAuthorization()
。
答案 1 :(得分:0)
import CoreLocation
class AppDelegate: CLLocationManagerDelegate{
var locationManager: CLLocationManager!
var currentCoordinate: CLLocationCoordinate2D?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.setupLocationManager()
return true
}
func setupLocationManager(){
locationManager = CLLocationManager()
locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager?.startUpdatingLocation()
}
// Below method will provide you current location.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if currentCoordinate == nil {
currentCoordinate = locations.last?.coordinate
locationManager?.stopMonitoringSignificantLocationChanges()
let locationValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locationValue)")
//currentCoordinate use this location Coordinate in your whole app.
locationManager?.stopUpdatingLocation()
}
}
// Below Mehtod will print error if not able to update location.
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error")
}
如果根据我的回答有任何疑问,请告诉我。
答案 2 :(得分:0)