Xcode 8: different entitlements for each scheme ca

2020-02-09 04:30发布

问题:

I found an issue with XCode 8 where .entitlements files are not being referenced properly for each scheme. Basically, my Debug .entitlements file is being referenced for my Release scheme. This is causing an issue because we implemented the new Rich push notification logic and that requires the use of App groups.

I am using two different teams (Development and Production), so there will be two specific App Groups.

Anyone know how to resolve this issue?

Thank you

回答1:

I found a solution. Make one .entitlements file add this:

<key>aps-environment</key>
<string>$(APS_ENVIRONMENT)</string>
<key>com.apple.security.application-groups</key>
<array>
    <string>$(APP_GROUP)</string>
</array>

Then in Target > Build settings Set the same .entitlements file in Signing > Code Signing Entitlements Add User-Defined Setting for APS_ENVIRONMENT and APP_GROUP setting the correct group for each target.

So, based on the target Xcode will use what you set for APS_ENVIRONMENT and APP_GROUP.

You can do this in plist too...did some amazing clean up today.



回答2:

Though Tim's solution mostly worked for me, Xcode got all upset and said automatic provisioning couldn't resolve issues with the entitlements file. I don't think it liked the variable.

Our solution was to:

  1. Enable all app groups on every target that needed access.

  1. Define a project-level user-defined setting called PROJECT_APP_GROUP for the app group name by going to Project -> Build Settings, clicking the "+" button, and selecting "Add User-Defined Setting."

  1. Set a variable in the info.plist file for each target that needs access to your app group.

  1. Then access the correct app group at runtime by getting the APP_GROUP variable from your target's info.plist file.

    + (NSString *)appGroupIdentifier
    {
        // this method returns the app group identifier by fetching it from the info.plist file.
        // this string is dynamic based on build scheme. for instance group.ourApp vs. group.ourApp-dev
        return [[[NSBundle mainBundle] infoDictionary] valueForKey:@"APP_GROUP"];
    }
    

OR

Now that I think of it, if you have preprocessor macros set up for each build, it might be easier to do something like:

+ (NSString *)appGroupIdentifier
{
#ifdef BUILD_DEV
    return @"group.myApp-dev";
#elif BUILD_STAGING
    return @"group.myApp-staging";
#else
    return @"group.myApp";
#endif
}