How to implement self signed certificates in Alamofire?

不问归期 提交于 2020-01-10 20:06:15

问题


I write a swift based app with a self signed certificate server communication. As a network library I want to use Alamofire. However Alamofire doesn't support self signed certificates.

Is it possible to implement this feature easily and how?


回答1:


This feature is not yet supported by Alamofire. It will most likely be added eventually by the community, but that work has yet to be submitted to the project. If you feel like contributing this feature, by all means fork the repo and submit a pull request.

If you want to learn how to implement this feature yourself, I'd suggest you read up on SSL pinning and TLS verification on iOS. You could also browse the source code in AFNetworking to get a feel of how it could possibly be implemented.

If you do not have time to build this feature yourself, then I suggest you use AFNetworking for the time being in your Swift app. AFNetworking is perfectly compatible with Swift and does support TLS verification.




回答2:


Alamofire now can be configured in this way:

let TollerantAlamofire={ ()->Alamofire.Manager in
    let policies:[String:ServerTrustPolicy]=[
        "www.mydemoserver.it": .DisableEvaluation
    ]

    let manager=Alamofire.Manager(serverTrustPolicyManager:ServerTrustPolicyManager(policies:policies))

    return manager

}()

Later, when you need to make a request, just use your configured instance like this:

var req:Request?
req=TollerantAlamofire
            .request(method, url, parameters: params)


来源:https://stackoverflow.com/questions/28868126/how-to-implement-self-signed-certificates-in-alamofire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!