ios - 如何在 ios 模拟器中禁用 ip https 验证?
问题描述
我正在使用 Ionic 4 创建一个 ios webview。我已经构建了 ios 平台,并且正在使用 iphone XR-12 模拟器在 xcode 上进行测试。我的应用程序使用自签名证书调用 iframe 到 ip https 测试。问题是ios不允许我访问这个ip,因为它是自签名的。我用 iframe 调用这个服务器,还使用从 httpclient ionic 访问它的 Web 服务。我无法访问服务器。
我已经尝试使用 NSAppTransportSecurity 添加异常
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>MI IP</key>
<dict>
<key>NSIncludesSubdomains</key>
<false/>
<key>NSExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowInsecureHTTPSLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>
但是在一个论坛中,我读到它不适用于 ip,并且我需要一个像 www.example.com 这样的域
所以我尝试修改主机文件,但我必须更改所有重定向,并且 css 和链接无法正常工作,所以我排除了编辑主机的可能性也尝试在模拟器中安装证书。我将证书拖到模拟器并安装它并在证书信任设置中激活它。但它不起作用
已经尝试在 AppDelegate.m 中添加此代码
@implementation NSURLRequest(DataController)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
也尝试在 CDVWKWebViewEngine.m 中添加此代码
- (void)webView:(WKWebView *)webView
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge completionHandler:(void (^)
(NSURLSessionAuthChallengeDisposition
disposition, NSURLCredential *credential))completionHandler {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
completionHandler(NSURLSessionAuthChallengeUseCredential,
[NSURLCredential credentialForTrust:serverTrust]);
}
还要在 config.xml 中添加这个
<preference name="CordovaWebViewEngine" value="CDVUIWebViewEngine"/>
但没有任何效果
在模拟器 ios 的日志中
任务 <837DAB71-F2D7-46C1-A29A-D1AD6736493F>.<2> 加载失败并出现错误错误域=NSURLErrorDomain Code=-1202“此服务器的证书无效。您可能正在连接到伪装成“ IP WEB SITE”,这可能会使您的机密信息面临风险。” UserInfo={NSLocalizedRecoverySuggestion=您还是要连接到服务器吗?, _kCFStreamErrorDomainKey=3, NSErrorPeerCertificateChainKey=( "" ), NSErrorClientCertificateStateKey=0, NSErrorFailingURLKey= https://IP WEB:4430/domesticas3p/registro, NSErrorFailingURLStringKey= https: //网页IP/domesticas3p/registro, NSUnderlyingError=0x7fa1a8e15650 {Error Domain=kCFErrorDomainCFNetwork Code=-1202 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=, _kCFNetworkCFStreamSSLErrorOriginalValue=-9843, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9843, kCFStreamPropertySSLPeerCer []
并且不要在 iframe 和调用 Web 服务的问题所在的地方显示任何内容
对不起我的英语,谢谢你的帮助:)
解决方案
我认为你的.plist
文件有倒退。NSAllowsArbitraryLoads
您首先通过设置为禁用 ATS true
,然后为您的 IP 创建一个例外,该例外不允许来自该 IP 的不安全加载。
NSExceptionAllowsInsecureHTTPLoads
还要注意正确的设置不应该NSExceptionAllowsInsecureHTTPSLoads
。
.plist
通过以下方式更改您的文件:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>your.ip.or.domain</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
这将使 ATS 保持打开状态,并为您的单个 IP 或域禁用它。
推荐阅读
- javascript - 如何替换从 react-native 中的数组获得的字符串
- python - 如何合并两帧?openai 健身房复古中闪烁的观察问题
- xml - 如何确定 xml 在代号中的格式是否正确?
- sql - 是否与空值定义的行为进行比较?
- java - HTML 音频标签在 localhost 中无法使用 apache tomcat
- postgresql - 错误:YAML 解析错误 - 将 YAML 转换为 JSON 时出错:yaml: control characters are not allowed
- java - 如何通过热敏打印机在卷筒上正确对齐打印多个标签
- python - 使用 multiprocessing.pool() 遍历列表
- windows - 是否可以在 windows iis 上运行 Nest.js 应用程序?
- c++ - 如何使用 C++17 的文件系统检查路径是否引用 Windows 中的卷?