2015-09-24 137 views
1

我幾天前完成了我的應用程序,並安裝了Xcode 7,這是一個痛苦的屁股,我有很多問題,但設法修復其中的大部分,但現在,當我的應用程序需要連接到互聯網,我得到這個奇怪的錯誤 這是我得到什麼記錄:應用程序運輸安全崩潰Swift 2.0應用程序

應用傳輸安全性已阻止明文HTTP(HTTP://)資源 負載,因爲它是不安全的。臨時例外可以通過 您的應用程序的Info.plist文件進行配置。 Hempel.temp_caseinsensitive_rename對 進行了優化編譯 - 步進可能表現異常;變量 可能不可用。

enter image description here

enter image description here

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

      var strData = NSString(data: data!, encoding: NSUTF8StringEncoding) 

      do { 
       let json = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as? NSDictionary 
       if let parseJSON = json { 
        //THIS IS WHERE ERROR IS in other ViewController 
        var success = parseJSON["data"] as! [String: AnyObject] 
        let item = success["hempel_antifoulings"] as! [[String: AnyObject]] 
        for i in item{ 

         let product = HempelAntifouling() 
         product.id = i["id"] 
         product.name = i["name"] 
         product.imageUrl = i["image"] 
         product.subgroup = i["subgroup"] 
         let url = NSURL(string: String(stringInterpolationSegment: product.imageUrl)) 
         let data = NSData(contentsOfURL: url!) 
         product.image = UIImage(data: data!) 
      // AND THIS IS WHERE THE ERROR POINTS in one of the ViewController 
         self.array.append(product) 
        } 

       }else{ 

        let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
        print("Error could not parse JSON: \(jsonStr)") 
       } 
      } catch { 
       // report error 
      } 

     }) 

     task.resume() 

    } 

附:這些代碼行在兩個ViewControllers大多相似,但錯誤是相同

+2

我當然,如果你谷歌'應用程序運輸安全',並做一些閱讀,你會發現它。 –

+3

http://stackoverflow.com/questions/30889312/api-call-error-in-xcode-7-ios-9-how-to-setup-app-transport-security-in-plist –

+0

對不起,我很害怕當我看到錯誤時,請通過 –

回答

2

下面是你應該在Info.plist什麼一個例子:

screen

此處瞭解詳情:https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/

+1

來了解一個重要的區別IMO:如果您接受通過HTTP加載不安全內容的責任,而不是HTTPS_,那麼您應該在Info.plist中擁有這些內容。您**應該**實際上做的是,如果它在您的控制範圍內,則是通過HTTPS加載您的內容。 –

+0

@CraigOtis謝謝,這是一個有價值的筆記! – rshev