2016-10-08 83 views
2

我正在使用Alamofire並嘗試從我的服務器獲取JSON Feed。服務器具有自簽名證書和通過用戶和密碼訪問。Alamofire 4 - Swift 3 - 全權證書和自簽名證書

這裏是我的代碼

let user = "user" 
    let password = "password" 

    let url1 = "https://10.0.1.2:4711/fhem/?cmd=jsonlist2&XHR=1" 
    let credential = URLCredential(user: user, password: password, persistence: .forSession) 

    let serverTrustPolicies :[String: ServerTrustPolicy] = [ 
    "10.0.1.8": .disableEvaluation 
    ] 


    let AlamoSession = SessionManager(serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)) 
    AlamoSession.request(url1) 
     .authenticate(usingCredential: credential) 
     .responseJSON { response in 
      debugPrint(response.response) 
      print("Result value \(response.result.value)") 
      //print (response.result.value?.valueForKey("status")) 
    } 

但它確實也沒有工作!

我該怎麼做?

在https地址上發出獲取請求wir自簽名證書?

在我的應用程序中的info.plist我添加了 應用交通運輸安全設置 - 允許任意負載 - YES

回答

1

我忽略了使用此代碼SSL錯誤。

import Alamofire 

struct WebAPI { 
    static let sessionManager: SessionManager = { 
     switch MyEnvironment.server { 
     case .development: 
      return Alamofire.SessionManager(
       serverTrustPolicyManager: ServerTrustPolicyManagerForDevelop() 
      ) 
     case .staging, .production: 
      return Alamofire.SessionManager() 
     } 
    }() 

    private class ServerTrustPolicyManagerForDevelop: ServerTrustPolicyManager { 

     init() { 
      super.init(policies: [:]) 
     } 

     override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? { 
      return .disableEvaluation 
     } 
    } 

}