为发布配置启用 NSAppTransportSecurity 并为 Debug/Staging 配置禁用它的安全可靠的方法?

Safe and reliable way to enable NSAppTransportSecurity for Release configuration and disable it for Debug/Staging configurations?

问题说明了一切。

我知道 NSAllowsArbitraryLoads 可以一起使用 NSExceptionDomains 但我对这种列入黑名单的方法感到有点困惑:我不想为除列出的特定生产主机之外的所有内容禁用 ATS NSExceptionDomains 因为它们可能会发生变化,所以我需要管理它们的列表以及一般应用程序配置,其中我们有 3 种以上不同的主机类型用于生产。 [当然,在一个完美的世界中,Apple 会建议我们列出我们要为其禁用 App Transport Security 的主机,并为所有其他主机启用它——不是!反之亦然]

我也尝试过继承我的自定义设置$(MY_USER_SETTING)(可以支持Debug/Staging/Release对应的3个不同的值)但是它对非字符串类型的[=15不适用=] 这是字典和 NSAllowsArbitraryLoads 布尔值——这些值不会继承我的用户定义设置。

背景:我希望能够使用 Charles Proxy 查看我们 Debug/Staging 配置的 HTTP 流量,从 iOS 9 开始,它需要禁用 ATS,我想确保这不会以任何方式影响我们的发布配置!

我想你已经有了答案。 所有主机默认启用 ATS。 如果您仅为暂存主机禁用它,那么您的生产端点不会受此影响。 也就是说,只需将您的暂存域添加到异常域即可。 还是我误解了你问题的具体内容?

与人们的想法相反(一个例子:WORKING WITH APPLE’S APP TRANSPORT SECURITYNSAllowsArbitraryLoads 不能用作在 blacklisting/whitelisting 模式之间切换的标志,至少它不能很好地与 Charles 一起使用:

黑名单方法(在 IOS 9.0 中对我不起作用 - Charles 无法识别流量 from/to 暂存主机):

Example B: ATS for all, with some exceptions

If you expect all of your domains to work with ATS, except a few that you know will not work, you can specify exceptions for where ATS should not be use, while leaving all other traffic opted in. For this scenario, you’ll want to use an NSExceptionDomains to specify the domains for which you wish to override ATS’s default settings.

白名单方法(行得通,但不是一个很好的方法): 如果 NSAllowsArbitraryLoads 设置为 YES 那么应用程序传输安全功能禁用所有域,NSExceptionDomains.

下列出的域除外

Example C: ATS disabled, with some exceptions

Conversely, you may only want ATS to work on domains you specifically know can support it. For example, if you developer a Twitter client, there will be countless URLs you may want to load that may not be able to support ATS, though you would want things like login calls, and other requests to Twitter to use ATS. In this case you can disable ATS as your default, then specify URL which you do wish to use ATS.


此处描述的另一种方法:This One Weird Trick Makes Developing iOS Apps Against a Local Server Way Easier 建议添加“运行 脚本构建阶段”,它使用 PlistBuddy 即时修补应用程序的 plist 文件。这是他们的示例,当开发人员在其本地计算机上针对服务器(当然也可以是暂存主机)工作时,使应用程序不使用 ATS:

/usr/libexec/PlistBuddy -c "Add :NSAppTransportSecurity:NSExceptionDomains:$LOCAL_NETWORK_NAME:NSIncludesSubdomains bool true" $INFO_PLIST
/usr/libexec/PlistBuddy -c "Add :NSAppTransportSecurity:NSExceptionDomains:$LOCAL_NETWORK_NAME:NSThirdPartyExceptionAllowsInsecureHTTPLoads bool true" $INFO_PLIST

IMO,与使用上述白名单方法相比,修补 Plist 是有条件地禁用暂存主机 ATS 的更好方法,因此我们将坚持使用 PlistBuddy。