安全设置似乎是正确的,但我的 React Native 应用程序由于某种原因无法连接到服务器(在 iOS 设备或模拟器上运行时都没有)。
服务器(IP 地址和端口)在网络上可见,因此错误可能是由 Info.plist
内的 NSAppTransportSecurity
引起的。重新启动它没有帮助。
一个可行的解决方案是将 NSAllowsArbitraryLoads
设置为 true
以打开地址。据我所知,它会暴露所有 IP 地址,因此应该避免。
NSExceptionDomains
似乎只适用于域名而不适用于 IP 地址。
如何打开 10.10.0.16
和 localhost
IP 地址并阻止所有其他地址?
应用传输安全设置:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSExceptionDomains</key>
<dict>
<key>10.10.0.16</key>
<dict>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSTemporaryExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSTemporaryExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
控制台日志:
2016-07-26 17:14:26.803 RNProject[80649:678556] NSMainNibFile and UIMainStoryboardFile are both set. NSMainNibFile ignored.
2016-07-26 17:14:26:936 RNProject[80649:678556] styleString = styleFile
2016-07-26 17:14:27.005 RNProject[80649:678837] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
2016-07-26 17:14:27.021 RNProject[80649:678556] INFO: Reveal Server started (Protocol Version 25).
2016-07-26 17:14:27.035 RNProject[80649:678556] Reachability: Reachable via WiFi
2016-07-26 17:14:27.035 RNProject[80649:678556] Reach = Reachability: Reachable via WiFi
2016-07-26 17:14:27.036 [fatal][tid:main] Could not connect to development server.
Ensure the following:
- Node server is running and available on the same network - run 'npm start' from react-native root
- Node server URL is correctly set in AppDelegate
URL: http://10.10.0.16:8081/index.ios.bundle?platform=ios&dev=true
最佳答案
似乎唯一的解决办法是设置<key>NSAllowsArbitraryLoads</key><true/>
在开发版本中加载 React Native 库并在发布时将其关闭。
关于iOS 应用传输安全 : Allow certain IP addresses in Info. plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38594029/