2015-09-06 89 views
31

我正在從事Xcode 7 beta 6。我試圖發送一個「刪除」請求http://mySubdomain.herokuapp.com應用交通安全Xcode 7 beta 6

我收到的錯誤是:

應用傳輸安全性已阻止明文HTTP(HTTP://)資源負載,因爲它是不安全的。臨時例外可以通過您的應用程序的Info.plist文件進行配置。
進行API調用時出錯:錯誤域= NSURLErrorDomain代碼= -1022由於App Transport安全策略要求使用安全連接,因此無法加載該資源。
NSLocalizedDescription =因爲應用傳輸安全策略要求使用安全連接的資源無法加載,NSUnderlyingError = {0x796f7ef0誤差區域= kCFErrorDomainCFNetwork代碼= -1022「(空)」}}

在我的實際API調用中,我放置了「https」而不是「http」,這實際上適用於我的POST請求。但是DELETE請求會拋出上述錯誤。

我在這裏看到了涉及pList文件的解決方案,但它們都不適合我。我在下面列出了我的嘗試。

第一次嘗試:

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
     <true/> 
</dict> 

第二個嘗試:

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSExceptionDomains</key> 
    <dict> 
     <key>herokuapp.com</key> 
     <dict> 
      <key>NSIncludesSubdomains</key> 
      <true/> 
      <key>NSExceptionAllowsInsecureHTTPLoads</key> 
      <true/> 
      <key>NSExceptionRequiresForwardSecrecy</key> 
      <false/> 
      <key>NSExceptionMinimumTLSVersion</key> 
      <string>TLSv1.2</string> 
      <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key> 
      <true/> 
      <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
      <false/> 
      <key>NSThirdPartyExceptionMinimumTLSVersion</key> 
      <string>TLSv1.2</string> 
      <key>NSRequiresCertificateTransparency</key> 
      <false/> 
     </dict> 
    </dict> 
</dict> 

最後,我甚至把所有這些臨時鑰匙,就像這樣:

<key>NSAppTransportSecurity</key> 
    <dict> 
     <key>NSExceptionDomains</key> 
     <dict> 
      <key>herokuapp.com</key> 
      <dict> 
       <key>NSIncludesSubdomains</key> 
       <true/> 
       <key>NSTemporaryIncludesSubdomains</key> 
       <true/> 
       <key>NSExceptionAllowsInsecureHTTPLoads</key> 
       <true/> 
       <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> 
       <true/> 
       <key>NSExceptionRequiresForwardSecrecy</key> 
       <false/> 
       <key>NSTemporaryExceptionRequiresForwardSecrecy</key> 
       <false/> 
       <key>NSExceptionMinimumTLSVersion</key> 
       <string>TLSv1.2</string> 
       <key>NSTemporaryExceptionMinimumTLSVersion</key> 
       <string>TLSv1.2</string> 
       <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key> 
       <true/> 
       <key>NSTemporaryThirdPartyExceptionAllowsInsecureHTTPLoads</key> 
       <true/> 
       <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
       <false/> 
       <key>NSTemporaryThirdPartyExceptionRequiresForwardSecrecy</key> 
       <false/> 
       <key>NSThirdPartyExceptionMinimumTLSVersion</key> 
       <string>TLSv1.2</string> 
       <key>NSTemporaryThirdPartyExceptionMinimumTLSVersion</key> 
       <string>TLSv1.2</string> 
       <key>NSRequiresCertificateTransparency</key> 
       <false/> 
       <key>NSTemporaryRequiresCertificateTransparency</key> 
       <false/> 
      </dict> 
     </dict> 
    </dict> 

所有沒有運氣!我總是得到同樣的錯誤。 DELETE請求格式正確,因爲當我從Postman手動執行時,我會得到所需的結果。

這裏是我的實際API調用方法的樣子,萬一有可能是這裏的問題:

class func makeDELETEALLRequest(completion: (error:Bool) -> Void) { 
     let session = NSURLSession.sharedSession() 
     let url = NSURL(string:"https://mysubdomain.herokuapp.com/42kh24kh2kj2g24/clean") 
     let request = NSMutableURLRequest(URL: url!) 
     request.HTTPMethod = "DELETE" 

     let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in 

      if (error != nil) { 
       print("Error making API call: \(error!)") 
       completion(error: true) 
      } else { 
       let HTTPResponse = response as! NSHTTPURLResponse 
       let statusCode = HTTPResponse.statusCode 
       if (statusCode == 200){ 
        print("Successfully deleted!") 
        completion(error: false) 
       } else { 
        print("Different status code: \(statusCode)") 
        completion(error: true) 
       } 
      } 
     } 
     task.resume() 
    } 

再次,我使用的Xcode 7測試版6

關於我選擇的答案 我選擇是正確的是適合我,因爲我改變了我的項目中的所有這些改變錯誤的plist文件並回答是,解決的可能性只有一個答案。其他答案提供的解決方案沒有錯,所以遇到此問題的其他人應該嘗試一下,因爲它們是有效的。我希望這可以幫助任何有類似問題的人。

+0

指導,以查看網頁URL的可接受性可https://medium.com/@Mrugraj/app-transport-security-b7910c4fc70f。這可能有助於瞭解未來 – Mrug

+0

首先嚐試在我的情況下工作任何方式,我覺得它有幫助,所以我有你的大拇指 –

回答

12

升級到xCode 7.0後,我也無法覆蓋App Transport Security,並嘗試使用相同類型的解決方案,但無濟於事。在離開它一段時間後,我注意到我已經對「MyAppName測試」的支持文件下的Info.plist進行了更改,而不是項目本身中的更改。我項目中的Supporting Files文件夾沒有展開,所以我甚至沒有注意到其中的Info.plist。

我敢肯定,典型的業餘錯誤,但它們在項目導航器中相隔僅有幾行,讓我感到沮喪,直到我注意到這個區別。以爲我會提到它,以防你遇到同樣的問題。

37

感激你嘗試添加以下內容,你的plist文件:

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict> 

...你可能想嘗試改變你的線路:

let url = NSURL(string:"https://mysubdomain.herokuapp.com/42kh24kh2kj2g24/clean") 

到:

let url = NSURL(string:"http://mysubdomain.herokuapp.com/42kh24kh2kj2g24/clean") 

道歉,如果你已經嘗試過。我可以理解,當你認爲自己已經耗盡所有途徑時,它是多麼令人沮喪。

但是,當我在Xcode 7上運行我的應用程序時,爲了測試我們的應用程序,一開始就出現了「應用程序傳輸安全性」問題。我們正在使用基於Oracle的Web服務,並且現在開始爲基於SSL的HTTP配置數字證書爲時已晚。所以,我的plist文件中的上述添加技巧就成功了。感謝你說你已經試過這個。但是,只是爲了幫助其他人,它確實爲我工作。它需要,因爲我沒有立即在Oracle盒子上啓用SSL。

44

我已經通過在info.plist中添加一些鍵來解決它。正如我對某些本地應用程序使用目標C.

我遵循的步驟是:

  1. 打開我的項目info.plist文件

  2. 增加了一個叫做NSAppTransportSecurity作爲Dictionary的關鍵。

  3. 添加了一個名爲NSAllowsArbitraryLoads的子項作爲Boolean,並將其值設置爲YES,如下圖所示。

enter image description here

清潔項目,現在一切都運行良好,爲像以前一樣。

參考鏈接:

  1. https://stackoverflow.com/a/32631185/2905967

  2. https://stackoverflow.com/a/32609970

4

我已經解決了的plist文件。加入NSAppTransportSecurity : Dictionary
2.添加子項名爲NSAllowsArbitraryLoadsBoolean : YES

這很好用。

+0

爲我工作很好 –

4

在Xcode中8

  1. 打開我的項目Info.plist文件
  2. 添加一個名爲AppTransportSecurity作爲字典的關鍵。
  3. 添加一個名爲AllowsArbitraryLoads的子項,並將其值設置爲YES,如下圖所示。

enter image description here