3

Good afternoon, for the past couple weeks I have been facing an issue in Xcode that says:

This app has attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain both “NSLocationAlwaysAndWhenInUseUsageDescription” and “NSLocationWhenInUseUsageDescription” keys with string values explaining to the user how the app uses this data

I have both of these usage descriptions implemented in my info-plist, I have tried deleting the app from my phone (I use my iPhone as a simulator) I have tried to reorganize my code, I have tried to comment out specific lines just to see if the message will go away and allow me to see my location. I have tried removing and reinstalling the google maps pods but nothing. I have tried to read up on this issue on StackOverflow, medium and GitHub to try and see if I can use previous tips to help solve my problem. I have even posted on here to see if I can get a little insight on this issue.

I have no idea what to do left to figure out this problem and I really don't want to start over. I will post the entirety of my code below, which is quite extensive. If someone has the free time to read through and let me know what I did wrong or did not implement, It would be greatly appreciated.

import UIKit
import GoogleMaps
import GooglePlaces
import CoreLocation

class mainViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate, GMSAutocompleteViewControllerDelegate, UITextFieldDelegate {

    let currentLocationMarker = GMSMarker()
    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.prefersLargeTitles = false

        navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 25)]


        myMapView.delegate=self
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
        locationManager.startMonitoringSignificantLocationChanges()
        locationManager.startUpdatingLocation()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        setupViews()
        initGoogleMaps()
        txtFieldSearch.delegate=self

    func initGoogleMaps() {
        let camera = GMSCameraPosition.camera(withLatitude: 40.014281, longitude: -83.030914, zoom: 17.0)
    self.myMapView.camera = camera
        self.myMapView.delegate = self
        self.myMapView.isMyLocationEnabled = true
    }
    func getLocation() {

        let status  = CLLocationManager.authorizationStatus()

        if status == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
            return
        }

        if status == .denied || status == .restricted {
            let alert = UIAlertController(title: "Location Services Disabled", message: "Please enable Location Services in Settings", preferredStyle: .alert)

            let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alert.addAction(okAction)

            present(alert, animated: true, completion: nil)
            return
        }
    }



    @objc func btnMyLocationAction() {
        let location: CLLocation? = myMapView.myLocation
        if location != nil {
            myMapView.animate(toLocation: (location?.coordinate)!)
        }
    }

    let myMapView: GMSMapView = {
        let v=GMSMapView()
        v.translatesAutoresizingMaskIntoConstraints=false
        return v
    }()


    let btnMyLocation: UIButton = {
        let btn=UIButton()
        btn.backgroundColor = UIColor.white
        btn.setImage(#imageLiteral(resourceName: "my_location-1"), for: .normal)
        btn.layer.cornerRadius = 25
        btn.clipsToBounds=true
        btn.tintColor = UIColor.gray
        btn.imageView?.tintColor=UIColor.gray
        btn.addTarget(self, action: #selector(btnMyLocationAction), for: .touchUpInside)
        btn.translatesAutoresizingMaskIntoConstraints=false
        return btn
    }()
}
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189

2 Answers2

1

Go search the app target's build settings for "info.plist" and study the full path very carefully. Make sure you added the keys to the correct info.plist - that it's the one actually being used officially as the info.plist for your app build. You can also click on the Info tab next to Build Settings to verify the two entries are there.

Here's how it should look (only be sure to provide a value for BOTH of them): enter image description here

Smartcat
  • 2,834
  • 1
  • 13
  • 25
  • Thank you. @smartcat that allows me to receive a push notification to allow the user to decided whether or not he or she would like to allow the app to access their location. But it doesn’t let me show their location on the map –  Oct 30 '18 at 03:00
  • I would think after allowing the app to access their location it would show. –  Oct 30 '18 at 03:02
  • Well you either haven't shown all your code or you forgot to override the methods of CLLocationManagerDelegate. Your class is set as the delegate, in this line: locationManager.delegate = self, but you need to actually override the methods. [This SO](https://stackoverflow.com/questions/24050633/implement-cllocationmanagerdelegate-methods-in-swift) has some examples. If you get stuck further, please post a new question as it sounds like this one is now solved. – Smartcat Oct 30 '18 at 03:08
  • Thank you @Smartcat. I didn't even need to show my code. I really hope others who are having the same problem can learn from this! –  Oct 30 '18 at 03:12
  • :) Glad to be of help! – Smartcat Oct 30 '18 at 03:13
0

All the reason for that is because you need to add a description in order to asking the user for location permissions. And in order to do that we have to go and edit the property list:

Under your Supporting Files folder in Xcode, We're going to add two new keys in your Info.plist ('Privacy-Location Always And When In Use Usage Description' and 'Privacy-Location When In Use Usage Description').

In order to do that, you go to the part where it says 'Information Property List', click on that little add button (+). And if you delete the 'Application Category' and start typing 'Privacy' (with a capital P), you'll start seeing the suggestions pop up.

After finding and choosing 'Privacy-Location Always And When In Use Usage Description' and 'Privacy-Location When In Use Usage Description', the next thing you have to do is give each of them a value. Under the 'Value' column, simply writing that description "We need your location to..." (...to obtain your current weather conditions, perhaps).

Now you have two new keys and they each have a value that is of data type String.

kpage
  • 1
  • In my info-plist I had all of the required information but for some reason it wasnt linked in info next to build settings. I had to put it in custom IOS target properties –  Oct 30 '18 at 04:21
  • It doesn’t let you show their location on the app or something? – kpage Oct 30 '18 at 04:30
  • It wasn’t letting me unfortunately not. It wouldn’t even send me a pop up notification asking for permission it just said I had to add it to my info-plist. After adding it to the custom targets it allowed me to! It’s a little off, like the blue dot is off to the right instead of the middle of the screen but I’ll live haha. –  Oct 30 '18 at 04:33
  • I think you should create a new project and start all over again following the instructions above, but not today, start working on it tomorrow. Trust me that would help you a lot. Good luck! – kpage Oct 30 '18 at 04:47