Search code examples
iosxcodeswift3mkmapview

Xcode Location access not allowed


I'm using Xcode 8.2.1 with Swift 3, and in my app there is a map view where the user is supposed to be able to press a button and the map will zoom in on their location.

In Info.plist, I added two rows: NSLocationAlwaysUsageDescription with value "asdf" and Privacy - Location When In Use Usage Description with value "fdas".

Before running the app, I choose "Simulate Location" and choose a preset location. However, whenever the user presses the button, the map zooms to some random place in the ocean and the following message is printed to the console:

The app's Info.plist must contain an NSLocationAlwaysUsageDescription key with a string value explaining to the user how the app uses this data

Here's my entire info.plist source code:

`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>Need your location </key>
    <string>asdf</string>
    <key>Need your location</key>
    <string>fdas</string>
</dict>
</plist>`

Here's the function as well:

> func zoomOnLocation(sender: UIBarButtonItem) {
        print("zoomOnLocation running")
        /* Tracks user location */
        var locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        mapView.showsUserLocation = true
        if (CLLocationManager.locationServicesEnabled()) { //Check for Location Services
            locationManager = CLLocationManager()
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
        }

        //Zoom to user location
        let noLocation = CLLocationCoordinate2D()
        let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200)
        mapView.setRegion(viewRegion, animated: false)

        DispatchQueue.main.async {
            locationManager.startUpdatingLocation()
        }

    }

Any help is much appreciated!


Solution

  • Sounds like you're mixing syntax in your info.plist. One of those entries you say you've added is written as a property list property, the other as source. When I right-click on my info.plist in an app using location services and then select Open As... and then Source Code I see these:

    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Need your location for this app!</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>This app needs your location</string>

    Making sure that your info.plist contains similar entries to these when viewed as Source Code should fix your issue.

    The equivalent entries in a property list would be Privacy - Location When In Usage Description and Privacy - Location Always Usage Description.