0

我使用谷歌地图在我app.I已经在info.plist中应用程序不请求位置服务权限何时在安装时使用?

隐私设置此 - 位置在使用的时候使用情况说明

,并在我的代码(主屏幕),我检查这样太:

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) 
    } 

但它在第一次安装应用程序时没有询问权限。任何帮助将appriciate。

+0

你添加 <!DOCTYPE的plist PUBLIC “ - //苹果// DTD PLIST 1.0 // EN”“HTTP ://! www.apple.com/DTDs/PropertyList-1.0.dtd“> 为了能够检测到您是否正在驾驶,我们需要访问您的位置。 在您的.plist文件中? –

+0

只需在您的appdelegates.swift文件中设置此代码 –

+0

@HimanshuMoradiya在AppDelagate中添加哪些代码? –

回答

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") 
} 

如果有任何查询根据我的答案告诉我。

+0

完美这是工作 –

+0

@AnushkaMadushan这是工作,那么为什么让你失望投票? –

+0

对不起,我该如何改变它 –

2

您正在设置Info.plist键用于在用户使用应用程序时访问该位置(即当它位于前台)时,但在代码中,只要应用程序正在运行(即始终)就请求权限, 。

你需要决定你想要的。如果您想始终能够访问用户的位置,请更改密钥Info.plist。如果您想要在应用程序位于前台时访问用户的位置,请改为将权限请求更改为requestWhenInUseAuthorization()

0

从iOS的10起我们需要给于的.plist文件的位置的权限,就像下面enter image description here

,只是检查你的应用程序的位置服务启用或不settings.Settings - >您的应用程序的名称 - >允许位置访问。永远不应该被选中。

从设备中删除您的应用程序并重新安装应用程序,然后它应该问你的位置权限。

相关问题