0

I'm using google map in my app.I have set this in info.plist

Privacy - Location When In Use Usage Description

and in my code(HomeScreen) i'm checking like this too:

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

But it doesn't ask permission when install the app for the first time. Any help would appriciate.

Anushka Madushan
  • 681
  • 2
  • 12
  • 31

3 Answers3

2

You're setting the Info.plist key for accessing the location when the user is using the application (i.e. when it's in the foreground), but in your code, you're requesting permission whenever the application is running (i.e. always).

You need to decide which you want. If you want to always be able to access the user's location, then change the Info.plist key. If you want to access the user's location when your application is in the foreground, then change the permission request to requestWhenInUseAuthorization() instead.

Jim
  • 72,985
  • 14
  • 101
  • 108
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")
}

If any query according my answer then tell me .

Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
0

From iOS 10 onward we need to give the location permissions in .plist file just like followingenter image description here

and just check your app's location services enable or not in settings.Settings -> your app name -> Allow location Access. Never should not be selected.

Remove your app from device and reinstall app again then it should ask you the location permission.

Venkat
  • 347
  • 3
  • 13