Xcode 9 制作Framework时添加自定义设置的方法及禁用问题解决
Hey there! Let's work through your questions about adding custom settings and variables when building a Framework in Xcode 9— I’ve dealt with this exact setup (and that annoying disabled button issue) before, so let’s break it down.
If the "Add User-Defined Setting" option is enabled, follow these straightforward steps:
- Open your Framework project, and make sure you’ve selected your Framework target (not the top-level project— this is a super common mix-up!)
- Switch to the Build Settings tab
- Look for the small + button in the top-left corner of the Build Settings pane
- Select Add User-Defined Setting from the dropdown
- Name your setting (e.g.,
FRAMEWORK_VERSION_SUFFIXorMY_CUSTOM_CONFIG) and set its value as needed. You can even add different values for Debug and Release configurations by clicking the disclosure triangle next to the setting name.
If that + button is grayed out, don’t panic— here are two reliable workarounds:
Workaround 1: Double-Check Your Selection
First, triple-confirm you’ve selected the Framework target, not the project itself. When you have the project selected instead of the target, the option to add user-defined settings is disabled by default. Click on your target in the left sidebar (under "Targets") and try again— this fixes the issue 90% of the time.
Workaround 2: Manually Edit the Project File
If selecting the target doesn’t help, you can manually add the setting directly to the project’s .pbxproj file:
- Close Xcode completely (important to avoid file conflicts)
- Find your
.xcodeprojfile in Finder, right-click it, and select Show Package Contents - Open the
project.pbxprojfile with a text editor (like VS Code or TextEdit— just make sure it’s not a rich text editor) - Search for your Framework target’s name to locate its
XCBuildConfigurationsections (you’ll see separate entries for Debug and Release usually) - Inside the
buildSettingsblock of the configuration(s) you want, add your custom setting in this format:MY_CUSTOM_VAR = "MyCustomValue"; - Save the file, then reopen Xcode. Your new user-defined setting should now appear in the Build Settings tab under the "User-Defined" section.
Bonus: Alternative for Custom Variables
If you just need a custom variable to use in your code or build scripts, you can also add it to the Preprocessor Macros section under Build Settings. For example, add APP_CUSTOM_KEY="xyz123" here— it’ll be available as a macro in your code, and you can reference it in scripts using $(APP_CUSTOM_KEY) too.
内容的提问来源于stack exchange,提问作者nehaSingh

