Swift: How to Make Https Request Using Server SSL Certificate

后端 未结 1 2012
执念已碎
执念已碎 2021-01-23 10:03

Hi I want to make Https Request in Swift. Currently im accessing local server through ip address. Local Server has one SSL Certificate by accessing the certificate want to make

相关标签:
1条回答
  • 2021-01-23 10:39

    IMPORTANT: DO NOT USE THIS IN PRODUCTION. Basically, with Alamofire, you can bypass the authentication for app development and testing purpose. Make sure you remove it before the app is on the App Store or in production:-

    func bypassURLAuthentication() {
            let manager = Alamofire.Manager.sharedInstance
            manager.delegate.sessionDidReceiveChallenge = { session, challenge in
                var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
                var credential: NSURLCredential?
                if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
                    disposition = NSURLSessionAuthChallengeDisposition.UseCredential
                    credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
                } else {
                    if challenge.previousFailureCount > 0 {
                        disposition = .CancelAuthenticationChallenge
                    } else {
                        credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
                        if credential != nil {
                            disposition = .UseCredential
                        }
                    }
                }
                return (disposition, credential)
            }
        }
    

    Thank You! Let me know if this helps. :)

    0 讨论(0)
提交回复
热议问题