免责声明:此解决方案应该是临时的,并且有文档记录,这样它就不会停留在软件的生产阶段,这只是为了开发.
对于iOS,你所要做的就是打开你的xcodeproject(在RN的iOS文件夹中),一旦打开了,就转到RCTNetwork.在该项目中,导航到RCTHTTPRequestHandler.M
在该文件中,您将看到这样一行:
#pragma mark - NSURLSession delegate
就在那一行之后,添加这个函数
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
瞧,您现在可以在没有有效证书的情况下对API进行不安全的调用.
这应该足够了,但如果你仍然有问题,你可能需要go 你的项目的信息.plist,左键点击它并 Select open as...源代码.
最后再加上
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>subdomain.example.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
所以你的文件看起来像这样
...
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>subdomain.example.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
</plist>
对于一个真正的生产就绪解决方案,https://stackoverflow.com/a/36368360/5943130该解决方案更好