Skip to content

Instantly share code, notes, and snippets.

@jplazcano87
Forked from edwardmp/gist:a0ffb3ace02ce4392b26
Created November 1, 2015 22:25
Show Gist options
  • Save jplazcano87/70fe49c9102481c96091 to your computer and use it in GitHub Desktop.
Save jplazcano87/70fe49c9102481c96091 to your computer and use it in GitHub Desktop.
Working example of accepting self-signed SSL certificate in Swift
import UIKit
import Foundation
class ViewController: UIViewController, NSURLSessionDelegate {
override func viewDidLoad() {
super.viewDidLoad()
httpGet(NSMutableURLRequest(URL: NSURL(string: "https://example.com")!))
}
func httpGet(request: NSMutableURLRequest!) {
var configuration =
NSURLSessionConfiguration.defaultSessionConfiguration()
var session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())
var task = session.dataTaskWithRequest(request){
(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if error == nil {
var result = NSString(data: data, encoding:
NSASCIIStringEncoding)!
NSLog("result %@", result)
}
}
task.resume()
}
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment