sgr*_*ove 19 javascript ssl ios react-native
我正在构建一个小型的ReactNative iOS应用程序.在其中,我使用fetch API向我控制的服务器发出一个简单的get请求,该服务器具有有效但自签名的SSL证书.可以理解,这会导致错误.
在iOS/JSC之间,我不确定如何(或者什么!)我可以为我的应用程序配置忽略此错误 - 到目前为止我发现的所有答案都与Objective-C解决方案有关,我正在寻找一些东西我可以与JSC环境一起使用.
我遇到了同样的问题.正如您所指出的,似乎让原生iOS应用程序使用自签名证书的唯一方法是编写/修改Objective-C代码,这对于使用React Native的JavaScript开发人员来说不是一个好方法.出于这个原因,我认为您的问题是X/Y问题,我建议首先使用自签名证书以不同的方法解决您的整体问题.
我使用真正的证书进行开发.以下是我使用SSL和React Native进行本地开发API的方法.它是免费且简单的.
./letsencrypt-auto certonly --standalone -d dev.my-domain.com -d api.dev.my-domain.com
/etc/letsencrypt/live/dev.my-domain.com
scp -r your_user@123.123.(...):/etc/letsencrypt/live/dev.my-domain.com ./
hosts
在要使用域的每台计算机上修改文件address=/dev.my-domain.com/192.168.(...)
整个网络(我推荐这个)此时,由于您正在为您正在访问的域使用真实可信证书,因此您的api现在将在浏览器和开发中的设备上受到信任!
免责声明:此解决方案应该是临时的并记录在案,以便它不会停留在软件的生产阶段,这仅用于开发.
对于iOS,您只需打开您的xcodeproject(在RN中的iOS文件夹中),打开后,转到RCTNetwork.xcodeproj,在该项目中,导航到RCTHTTPRequestHandler.m
在该文件中,您将看到如下所示的行:
#pragma mark - NSURLSession delegate
Run Code Online (Sandbox Code Playgroud)
在该行之后,添加此功能
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
Run Code Online (Sandbox Code Playgroud)
瞧,您现在可以在没有有效证书的情况下对API进行不安全的调用.
这应该足够了,但是如果你仍然遇到问题,你可能需要转到项目的info.plist,左键单击它并选择打开为...源代码.
最后加入
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Run Code Online (Sandbox Code Playgroud)
所以你的文件看起来像这样
...
<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>
Run Code Online (Sandbox Code Playgroud)
对于真正的生产就绪解决方案,/sf/answers/2545785231/该解决方案更好
归档时间: |
|
查看次数: |
14000 次 |
最近记录: |