Server Trust Code:
func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void){
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
{
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential);
}
else
{
completionHandler(NSURLSessionAuthChallengeDisposition.CancelAuthenticationChallenge, nil)
}
}
In Plist we can add below code:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>"Your url"</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
</dict>
</dict>
Using
let parameters = ["UserEmail":userMailID, "DeviceUDID": stringUDID] as Dictionary<String, String>
let url = NSURL (string: "Your URL") //change the url
let request = NSMutableURLRequest(URL: url!)
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: nil, delegateQueue: NSOperationQueue.mainQueue())
request.HTTPMethod = "POST" //set http method as POST
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(parameters, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
// request.timeoutInterval = 100
let task = session.dataTaskWithRequest(request)
{
data, response, error in
guard data != nil else
{
// Error response
}
//RESPONSE HEADER RESULT
let httpsResponse = response as? NSHTTPURLResponse
let str = httpsResponse!.allHeaderFields as NSDictionary
let tokenStr = str["TokenAPI"] as! String
GlobalData.sharedManager.tokenAPI = tokenStr
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
self.json = try NSJSONSerialization.JSONObjectWithData ((jsonStr?.dataUsingEncoding(NSUTF8StringEncoding)!)!, options: .AllowFragments)
Print(self.json)
}
task.resume()
No comments:
Post a Comment