当项目设置了 NSExceptionDomains, 不管 NSAllowsArbitraryLoads 是 true/false, NSExceptionDomains 下面 NSExceptionAllowsInsecureHTTPLoads 的值决定了是否允许 http 请求
以阿里云的域名为例, 访问http://aliyuncs.com
以下两种情况允许 http 请求
- 允许个别域名 http 请求:
(1) NSAllowsArbitraryLoads: false,
(2) NSExceptionDomains内NSExceptionAllowsInsecureHTTPLoads: true
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<flase/>
<key>NSExceptionDomains</key>
<dict>
<key>aliyuncs.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
- 允许所有 http 请求:
(1) NSAllowsArbitraryLoads: true
(2) 不设置NSExceptionDomains
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
以下两种情况禁止 http 请求
- 禁止个别域名 http 请求:
(1) NSAllowsArbitraryLoads: true,
(2) NSExceptionDomains内NSExceptionAllowsInsecureHTTPLoads: false
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>aliyuncs.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<false/>
</dict>
</dict>
</dict>
- 禁止所有 http 请求:
(1) NSAllowsArbitraryLoads: false
(2) 不设置NSExceptionDomains
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
</dict>
总结:
NSExceptionDomains 的作用级别高于 NSAllowsArbitraryLoads
NSExceptionDomains 既可以是白名单, 也可以是黑名单