Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read entitlements files in swift

I am implementing universal deep linking in my app. When I registered my different domains, it creates an AppName.entitlements file

I would like to read the values of this file like a plist.

I tried

if let path = NSBundle.mainBundle().pathForResource("AppName", ofType:
"entitlements") { }

but it returns nil

Is it possible to read such files?

like image 426
kschaeffler Avatar asked May 17 '16 15:05

kschaeffler


People also ask

How do I check my entitlements?

Check the Entitlements In Your Build Log and App You'll find the name of the provisioning profile Xcode used to sign your app in the invocation of the codesign tool's command-line parameters. This command prints a plist that contains all of the entitlements built into the app.

What is entitlement file in iOS?

Entitlements are special app capabilities and security permissions granted to applications that are correctly configured to use them. In iOS, apps run in a sandbox, which provides a set of rules that limit access between the application and certain system resources or user data.


3 Answers

The solution I found:

add the appGroup into Info.Plist file

<key>appGroup</key>
<string>group.com.acronis.mobile.ios.development</string>

To read the string use the code below:

extension String {

    // MARK: - Static

    static var appGroup: String = {
        guard let bundleAppGroup = Bundle.main.infoDictionary?["appGroup"] as? String else {
            fatalError("The application must contain appGroup in Info.plist file")
        }
        return bundleAppGroup
    }()
}

Change the entitlements and Info.plist at before build time using your custom script if needed.

like image 37
Vyacheslav Avatar answered Nov 15 '22 06:11

Vyacheslav


That file isn't copied in to your app (see Xcode's target checkbox). It is only used for building

the entitlements are a config file for Xcode

so: no

like image 166
Daij-Djan Avatar answered Nov 15 '22 06:11

Daij-Djan


I dove into this subject a bit and it is actually quite simple.

You use a run script to grab the entitlements and merge it into Info.plist:

ENTITLEMENTS= # Path to Project.entitlements #
INFOPLIST= # Path to Info.plist #
echo "Writing entitlements to Info.plist"; 
KEY="entitlements-from-script"; 
/usr/libexec/PlistBuddy -c "delete $KEY" "$INFOPLIST"; 
/usr/libexec/PlistBuddy -c "add $KEY dict" "$INFOPLIST"; 
/usr/libexec/PlistBuddy -c "merge $ENTITLEMENTS $KEY" "$INFOPLIST";

Now you can use it in code:

let entitlements = Bundle.main.infoDictionary?["entitlements-from-script"]
like image 24
Casper Zandbergen Avatar answered Nov 15 '22 08:11

Casper Zandbergen