1

It is my general ViewController class , I research the this question iOS app doesn't ask for location permission , I already have NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription

@IBOutlet weak var ownMapView: MKMapView!

let locationManager : CLLocationManager = CLLocationManager();


override func viewDidLoad() {
    super.viewDidLoad();

    self.locationManager.delegate = self;
    self.locationManager.requestWhenInUseAuthorization();
    self.locationManager.startUpdatingLocation();
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    guard status == .AuthorizedWhenInUse else
    {
        print("Location not using");
        return;
    }

    print("Location using.");
    ownMapView.showsUserLocation = true;
    }

Even I added NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription this is not working for me . It prints only "Location not using"

How do I request correctly ?

Community
  • 1
  • 1
eemrah
  • 1,603
  • 3
  • 19
  • 37
  • At first run the status should be "NotDetermined" and you should get a permission popup. Did you get that popup? – firstinq Aug 23 '16 at 16:55
  • The first trying I choose the not Allowing option , then the program never ask the location allow request . – eemrah Aug 23 '16 at 16:57
  • I mean first running of the application – eemrah Aug 23 '16 at 16:57
  • Try by removing the app from device/simulator and run it again – firstinq Aug 23 '16 at 16:59
  • I am now trying to remove and run it again – eemrah Aug 23 '16 at 17:03
  • Sir , thanks to answer , I tried and it worked for me , I checked your answer as a Correct but i have another Question . When ı retry the app , firstly print the Location Not using then I allowed the Request then it prints Location using . What do you think about this situation ? – eemrah Aug 23 '16 at 17:07
  • Happy to help! I am posting the reply as an answer shortly, accept if it helps. – firstinq Aug 23 '16 at 17:10
  • Yes , I am waiting for this , Thanks for help ! – eemrah Aug 23 '16 at 17:11

2 Answers2

0

enter image description hereHave us added NSLocationAlwaysUsageDescription & NSLocationWhenInUseUsageDescription in plist? . Add these and it will work .

Shemona Puri
  • 803
  • 7
  • 12
0

To understand this correctly check the CLLAuthorizationStatus doc: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/#//apple_ref/c/tdef/CLAuthorizationStatus

CLAuthorizationStatus has several possible values:

  • NotDetermined - User has not yet made a decision to allow/deny to use location
  • Restricted - App is restricted to use location
  • Denied - User has denied to use location.
  • AuthorizedAlways - This app is authorized to start location services at any time.
  • AuthorizedWhenInUse - App is authorized to start most location services while running in the foreground

You are checking only "AuthorizedWhenInUse" status but you launch first its status is NotDetermined.

You can further check the status like below:

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

        if status  == .NotDetermined
        {
            print ("Loction use not determined")
            return
        }
        if status  == .Denied
        {
            print ("Location determined")
            return
        }
        guard status == .AuthorizedWhenInUse else
        {
            print("Location not using");
            return;
        }

        print("Location using.");
        ownMapView.showsUserLocation = true;
    }
firstinq
  • 772
  • 6
  • 13