xcode 从用户定义的设置在 Info.plist 中设置布尔属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1832157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:59:01  来源:igfitidea点击:

Setting a boolean property in Info.plist from a User Defined Setting

iphonexcodemacos

提问by edoloughlin

It's simple to set a property in an Info.plist file from a user defined setting, you just use ${YOUR_SETTING_NAME} as the value. However, is it possible to do this for a bolean property? The structure in the plist file for a boolean is:

从用户定义的设置在 Info.plist 文件中设置属性很简单,您只需使用 ${YOUR_SETTING_NAME} 作为值。但是,是否可以为 bolean 属性执行此操作?plist 文件中布尔值的结构是:

<key>NSAppleScriptEnabled</key>
<false/>

It's not clear how to use a user defined setting here.

这里不清楚如何使用用户定义的设置。

采纳答案by Ken Aspeslagh

I'm not sure how to do what you're asking, but I'm pretty sure that for this particular key (NSAppleScriptEnabled) you can also use strings "YES" and "NO" and it will work.

我不确定如何执行您的要求,但我很确定对于这个特定的键 (NSAppleScriptEnabled),您还可以使用字符串“YES”和“NO”,它会起作用。

回答by gasparuff

plist files containing booleans within tags are not valid any more.

在标签中包含布尔值的 plist 文件不再有效。

This solution works:

此解决方案有效:

Add a new Run Script Build Phase to your target. Put in this:

向目标添加新的运行脚本构建阶段。把这个:

if [ ${CONFIGURATION} = "Release" ]; then
/usr/libexec/PlistBuddy -c "Set :UIFileSharingEnabled NO" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
fi

So in my configuration, I'm setting the UIFileSharingEnabled to YES in my plist as a default and when I'm building for Release, then the step above happens and sets it to false.

因此,在我的配置中,我将 plist 中的 UIFileSharingEnabled 设置为 YES 作为默认值,当我为 Release 构建时,会发生上述步骤并将其设置为 false。

回答by Louis Gerbarg

What do you mean by "User Defined Setting" ...

“用户定义设置”是什么意思...

If the user you are talking about is you (in other words, the app's developer), then you can just put whatever keys you want there, just like any other plist in your Xcode project.

如果您正在谈论的用户是您(换句话说,应用程序的开发人员),那么您可以将任何您想要的键放在那里,就像您的 Xcode 项目中的任何其他 plist 一样。

If the user you are talking about is your app's end user, don't try to save their settings in your Info.plist. It is a part of the application. While it is sometimes possible for an app to change its own info plist on a mac, it often is not, depending on how the app was installed. On the iPhone it is never possible, since the app is read-only. In either event, changing your Info.plist would invalidate any app signing you have done.

如果您谈论的用户是您应用的最终用户,请不要尝试将他们的设置保存在您的 Info.plist 中。它是应用程序的一部分。虽然有时应用程序可以在 Mac 上更改自己的信息 plist,但通常不会,这取决于应用程序的安装方式。在 iPhone 上这是不可能的,因为该应用程序是只读的。无论哪种情况,更改您的 Info.plist 都会使您所做的任何应用签名无效。

If you want to change end user settings, use something NSUserDefaults.

如果要更改最终用户设置,请使用 NSUserDefaults。