【问题标题】:How to disable ip https validation in the ios simulator?如何在 ios 模拟器中禁用 ip https 验证?
【发布时间】: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


    【解决方案1】:

    我认为您的 .plist 文件有倒退。您首先通过将 NSAllowsArbitraryLoads 设置为 true 来禁用 ATS,然后为您的 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 或域禁用它。

    【讨论】:

      【解决方案2】:

      我编辑了 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]);

      }

      【讨论】:

        猜你喜欢
        • 2020-10-19
        • 2022-01-26
        • 2018-04-19
        • 2016-05-30
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 1970-01-01
        • 2021-10-08
        相关资源
        最近更新 更多