簡體   English   中英

iOS App傳輸安全性異常不起作用

[英]iOS App Transport Security exceptions not working

我正在嘗試連接到iOS 9.1上的自簽名HTTPS服務器。

我正在使用 :

[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

而且我收到一條錯誤消息,指出證書無效:

Error Domain=NSURLErrorDomain Code=-1202 "<French error saying the certificate is invalid" 
UserInfo=
   {NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x13c768140>, 
    NSLocalizedRecoverySuggestion=<French recovery suggestion asking if the user wants to connect anyway>, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, NSErrorPeerCertificateChainKey=<CFArray 0x13d82ffe0 [0x1a1242b68]>

我嘗試添加所有可能的傳輸安全異常,我目前的嘗試是:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>mydomain.org</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>

我也嘗試了NSAllowsArbitraryLoads但是那還是行不通的。 我有幾個.plist文件(cocoapods和測試),我認為它是正確的文件(App / Supporting Files / Info.plist)。

有什么問題的想法嗎?

您需要像這樣在最后添加

<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

因此,這是一個NSURLConnection問題,而不是應用程序傳輸問題。

對於記錄,以及對於那些可能遇到相同問題的人,我已經通過調用私有方法來臨時修復( 不要這樣做 ):

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;

蘋果不會接受 真正的解決方案是使用委托方法 ,當我們要解決這個問題時,我將編輯此答案(我的客戶是“開發人員”,所以我使用了他的選擇,他說,我們稍后會發現,我們正在匆忙...)

我已按照以下步驟解決了問題

  1. 打開項目的plist文件
  2. 添加密鑰“ NSAppTransportSecurity”並在類型字典上
  3. 將內部鍵“ NSAllowsArbitraryLoads”添加為布爾值YES

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM