AFNetworking 3 issue

2019-05-29 08:28发布

In AFNetworking 3 for invalid SSL certificate I used validatesCertificateChain = false , but now it seems that this field was removed and I can't make requests to my server.

Here is class for requests:

import UIKit
import AFNetworking

class ClientHTML: AFHTTPSessionManager {
    private static var __once: () = { () -> Void in

        let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.certificate)
        securityPolicy.validatesDomainName = false
        securityPolicy.allowInvalidCertificates = true
        sharedInstanceTemp.securityPolicy = securityPolicy


        sharedInstanceTemp.requestSerializer = AFHTTPRequestSerializer()
        sharedInstanceTemp.responseSerializer = AFHTTPResponseSerializer()


    }()

    fileprivate static let sharedInstanceTemp = ClientHTML(baseURL: URL(string: kServer_urlBilderlings))

    static var sharedInstance:ClientHTML {
        get {
            _ = ClientHTML.__once

            return sharedInstanceTemp
        }
    }
}

I do requests by:

ClientHTML.sharedInstance.post("https://acs-web-test.firstdata.lv", parameters: nil, progress: { (progress) in
            print("progress = ", progress)
        }, success: { (task, response) in
            let data = response as! Data
            let html = String(data: data, encoding: .utf8)
            print("success responce = ", html)
        }, failure: { (task, error) in
            print("error = ", error)
        })

And according to this post I have to use validatesCertificateChain property. Can anybody help?

2条回答
SAY GOODBYE
2楼-- · 2019-05-29 09:15

I solved it by changing let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.certificate) to let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.none)

查看更多
手持菜刀,她持情操
3楼-- · 2019-05-29 09:17

For objective-c user setAllowInvalidCertificates:YES without SSL Pinning:

AFSecurityPolicy *sec = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[sec setAllowInvalidCertificates:YES];
[sec setValidatesDomainName:NO];
manager.securityPolicy = sec;
查看更多
登录 后发表回答