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.
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)
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?