当我使用XMLHttpRequest连接非有效证书的https服务器时,React Native会抛出异常"The operation couldn’t be completed. (NSURLErrorDomain error -1202.)"。
是否有可能禁用React Native XMLHttpRequest的ssl警告/检查?
当我使用XMLHttpRequest连接非有效证书的https服务器时,React Native会抛出异常"The operation couldn’t be completed. (NSURLErrorDomain error -1202.)"。
是否有可能禁用React Native XMLHttpRequest的ssl警告/检查?
#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进行不安全的调用。
这应该足够了,但如果您仍然遇到问题,可能需要前往项目的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://dev59.com/MFwY5IYBdhLWcg3wM1bK#36368360这个方案更好。
但是,您可以通过编辑应用程序的Info.plist文件来覆盖默认行为。
有许多不同的标志可以设置,它们都在{{link1:苹果官方技术说明}}中进行了描述。
最简单的修改方法是完全关闭ATS,如下所示:
Open as > Source Code
<dict>
标记下面添加以下代码如果您计划发布应用程序,请不要留下此处。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
fetch
而不是 XHR? - Richard Kho