免责声明:此解决方案应该是临时的并记录在案,以便它不会停留在软件的生产阶段,这仅用于开发。
对于 iOS,您所要做的就是打开您的 xcodeproject(在 RN 中的 iOS 文件夹中),然后转到 RCTNetwork.xcodeproj 并在该项目中导航到 RCTHTTPRequestHandler.m
在该文件中,您将看到如下一行:
#pragma mark - NSURLSession delegate
在那一行之后,添加这个函数
#if DEBUG
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
#endif
瞧,您现在可以在没有有效证书的情况下对您的 API 进行不安全的调用。
这应该足够了,但是如果您仍然遇到问题,您可能需要转到项目的 info.plist,左键单击它并选择打开为...源代码。
最后添加
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</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>
</dict>
</plist>
对于真正的生产就绪解决方案,https://stackoverflow.com/a/36368360/5943130 该解决方案更好