Info.plist : add new key

In previous versions of Xcode, an Info.plist file was used for custom settings and keys in an Xcode project. This file isn’t present in modern versions of Xcode, yet there are many tutorials and blogs around that rely on it. Some explain what to do in lieu of the infamous “missing Info.plist” file but, as a newbie, I didn’t find these explanations helpful. I did ultimately discover for myself what needed to be done, so I’m listing the steps taken here for future reference.

Use Case:

import Foundation
import CoreLocation

class ContentModel: ObservableObject {
    
    var locationManager = CLLocationManager()
    init() {
        locationManager.requestWhenInUseAuthorization()
    }
    ...
}

If you attempt to use requestWhenInUseAuthorization(), Xcode will fail the simulation build with the error:

This app has attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an “NSLocationWhenInUseUsageDescription” key with a string value explaining to the user how the app uses this data.

The error tells you that the Info.plist file must contain that key with a message for the user – except that modern versions of Xcode don’t have Info.plist, so you can’t do what the error tells you to do.

Many websites will tell you how to add an Info.plist, and I followed their advice. But, if you do this, Xcode will fail the simulation build with this error:

duplicate output file ‘/Users/…/Library/Developer/Xcode/DerivedData/M6-CitySightsApp-bzsrbsbedgfeluejtjqoqawqsnkr/Build/Products/Debug-iphonesimulator/M6-CitySightsApp.app/Info.plist’ on task: ProcessInfoPlistFile /Users/…/Library/Developer/Xcode/DerivedData/M6-CitySightsApp-bzsrbsbedgfeluejtjqoqawqsnkr/Build/Products/Debug-iphonesimulator/M6-CitySightsApp.app/Info.plist /Users/…/.../My Learning/CWC+/02 – iOS Foundations 2021/modules/Module 06 – City Sights App/M6-CitySightsApp/M6-CitySightsApp/Info.plist

So, without Info.plist you don’t have the required key for the build to succeed, but if you create an Info.plist then you’re told you have a duplicate of Info.plist. Crazy Stuff!

The only information I could find was the “official solution from Apple”, which doesn’t quite nail it for me:

Project > Targets > Info [tab] > Custom iOS Target Properties

You’ll find yourself on the screen below with no further help. Some helpful sites will say to “edit properties”, but the key we want doesn’t exist to edit, and there is no intuitive way to create it. There’s no ‘add key’, or + button that will do this.

Info Panel

TL;DR : The solution!

The solution to adding the key is as follows:

The steps required to create our new key, and have it added to Info.plist when the app is built.

Step 1

Select the Project Name of your App.

Step 2

Click the ‘Info’ tab.

Step 3

If you move your mouse down the list of up/down buttons in the “Custom iOS Target Properties” list, you’ll see a + and – icon appear.

Click the + icon to add a new key.

Step 4

Either scroll down the list, or start typing the key that we want to add, such as:

“Privacy – Location When In Use Usage Description”

Step 5

At the right-hand side of the new key (just beyond the “String” word in grey), add the message we want to show the user, such as:

“Grant permission to find restaurants and sights near you.”

Remember to save your project when done.

Step 6

When built, Xcode’s simulator should now prompt for user permissions with your message.

And that’s it, job done!

%d bloggers like this: