Disable HTTPS GET certificate check in Swift 5
If you really want to ignore the SSL certificate you could, for example, ignore it using a method exposed by the URLSessionDelegate
as follows:
extension YOURVIEWCONTROLLER: URLSessionDelegate {
public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
//Trust the certificate even if not valid
let urlCredential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, urlCredential)
}
}
Remember to set the delegate to self
when you create the URLSession
in your code. For example, you can set it using this:
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
I hope it helps
Disable HTTPS GET certificate check
add this entry to info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>server.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSAllowsLocalNetworking</key>
<true/>
</dict>
and use the HTTP insteasd of HTTPS in URL
iOS https / Host dont't has SSL certificate
You need to allow Allow Arbitrary Loads
. In your info.plist file, add this following entry
How do I accept a self-signed SSL certificate using iOS 7's NSURLSession
Both connection:canAuthenticateAgainstProtectionSpace:
and connection:didReceiveAuthenticationChallenge:
are deprecated in iOS 8 anyway so you should use other methods.
What I am using in my projects is a delegate method of NSURLSessionDelegate. Adhere to that protocol then add this method:
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust))
}
Then, when you use initialize NSURLSession with delegate set to self. For example:
var session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())
Then use that session instance to call dataTaskWithRequest method on:
var task = session.dataTaskWithRequest(request){
(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if error != nil {
callback("", error.localizedDescription)
} else {
var result = NSString(data: data, encoding:
NSASCIIStringEncoding)!
}
}
task.resume()
Complete working example can be found here.
For security reasons, if you use a self-signed certificate I recommend also implementing public key pinning (https://gist.github.com/edwardmp/df8517aa9f1752e73353)
Swift SSL error with self signed certificate
@Ashish Kakkad was spot on. This works:
class Blah: NSURLSessionDelegate {
func rest() {
let path = "https://localhost:8443/greeting"
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let json:JSON = JSON(data: data!)
if let c = json["content"].string {
print(c)
}
})
task.resume()
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
}
}
With this in the Info.plist file:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
Related Topics
Swift 4.2, String Firstindex() Function Error in Xcode Playground
How to Add Icon to a Share Sheet in Swift
Swift Spritekit I Detect a Collison But It Reads the Collision Mulitple Times
Why Does Somestruct() Is Anyobject Return True
Swift: Dictionaries Inside Array
Given a Function Parameter of Type [Int]; Can It Be Constrained to Not Be Empty
Cast Any to Float Always Fails in Swift4.1
How to Find the Time Interval Remaining from Nstimer
With Swiftui, How to Constrain a View's Size to Another Non-Sibling View
Xcode: Using Custom Fonts Inside Dynamic Framework
Why Can't I Use Subscripting on a Ckrecord Object in Swift
Why Doesn't Swift Force My Designated Initializer to Call Super
Data Ranged Subscribe Strange Behavior
Text from [String] to a Label.Text Isn't Working the First Time
How to Convert Dispatchtimeinterval to Nstimeinterval (Or Double)
How to Use Uiimage(Contentsoffile:String) Method to Load Images from Images.Xcassets Folder