How do I implement certificate pinning with URLSession in Swift?

Certificate pinning is a security measure used to prevent man-in-the-middle attacks by hardcoding the server's certificate in the app. Implementing certificate pinning in a Swift application using URLSession can help ensure that your app communicates securely with your server.

How to Implement Certificate Pinning with URLSession in Swift

Below is an example of how to implement certificate pinning using Swift's URLSessionDelegate. This will ensure that your app only accepts specific certificates when communicating with your server.

import Foundation class NetworkManager: NSObject, URLSessionDelegate { func startRequest() { let url = URL(string: "https://yourapi.com/endpoint")! var request = URLRequest(url: url) request.httpMethod = "GET" let session = URLSession(configuration: .default, delegate: self, delegateQueue: nil) let task = session.dataTask(with: request) { data, response, error in if let error = error { print("Error: \(error)") return } // Handle the response if let data = data, let responseString = String(data: data, encoding: .utf8) { print("Response: \(responseString)") } } task.resume() } // URLSessionDelegate method to perform certificate pinning func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { if let serverTrust = challenge.protectionSpace.serverTrust { // Load your pinned certificate let certPath = Bundle.main.path(forResource: "your_certificate", ofType: "cer")! let certData = NSData(contentsOfFile: certPath)! let pinnedCertificate = SecCertificateCreateWithData(nil, certData)! let serverCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0)! if pinnedCertificate == serverCertificate { completionHandler(.useCredential, URLCredential(trust: serverTrust)) return } } } completionHandler(.performDefaultHandling, nil) } }

Certificate Pinning URLSession Swift iOS Security Network Security Man-in-the-Middle Attack Prevention