【发布时间】:2020-01-03 23:31:18
【问题描述】:
我正在使用 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 的日志中
任务 . 加载失败并出现错误 Error Domain=NSURLErrorDomain Code=-1202 “此服务器的证书无效。您可能正在连接到假装的服务器成为可能使您的机密信息面临风险的“IP WEB SITE”。” UserInfo={NSLocalizedRecoverySuggestion=还是要连接到服务器吗?, _kCFStreamErrorDomainKey=3, NSErrorPeerCertificateChainKey=( “” ( _kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=, _kCFNetworkCFStreamSSLErrorOriginalValue=-9843, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9843, kCFStreamPropertySSLPeerCer []
并且不要在 iframe 所在的地方显示任何内容以及调用 Web 服务的问题
对不起我的英语,谢谢你的帮助:)
【问题讨论】:
标签: ios ionic-framework ios-simulator