0

I have been trying to connect my ios application to my restful web API through http basic authentication but I am unable to connect. Here is my code:

let URL = NSURL(string:"https://devWebsvc1.whateverYolo.local:11201/api/webcall")
let theRequest = NSMutableURLRequest(URL:URL)
let session = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())
theRequest.HTTPMethod = "POST"
theRequest.addValue("application/json", forHTTPHeaderField:"Content-Type")
theRequest.addValue("application/json", forHTTPHeaderField:"Accept")


let credential = NSURLCredential(user:"username", password:"password", persistence: NSURLCredentialPersistence.ForSession)
let protectionSpace = NSURLProtectionSpace(Host: URL?.host)!, port:11201, 'protocol': URL?.scheme, realm: nil, authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
let credentialStorage = NSURLCredentialStorage.sharedCredentialStorage()
credentialStorage.setDefaultCredential(credential,forProtectionSpace:protectionSpace)
theSession.configuration.URLCredentialsStorage = credentialStorage

let task = theSession.dataTaskWithRequest(theRequest, completionHandler : {data, response, error -> Void in 
if error != nil
{print("\(error)")}})

Error message is :-

Optional(Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, NSErrorPeerCertificateChainKey={type = immutable, count = 1, values = ( 0 : )}, NSUnderlyingError=0x7fe023520170 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, kCFStreamPropertySSLPeerCertificates={type = immutable, count = 1, values = ( 0 : )}}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://devwebsvc1.whateverYolo.local:11201/api/Device, NSErrorFailingURLStringKey=https://devwebsvc1.whateverYolo.local:11201/api/Device, NSErrorClientCertificateStateKey=0})

Do you guys know what could be the error? Any help is appreciated. I am using xcode 7 by the way.

TryinHard
  • 4,078
  • 3
  • 28
  • 54
  • Does your server handle TLS 1.2 secure protocols? That is now required in iOS 9. If this is the problem you can look here for a solution. http://stackoverflow.com/a/30748166/1671729 – Putz1103 Sep 28 '15 at 19:19

1 Answers1

0

Error may be in this line :-

theRequest.addValue("application/json", forHTTPHeaderField:"Accept")

You should go to this google extension and find what's the value for the Field "Accept"

chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html

enter image description here

add Your url ,username and password and click submit and find all of your Header Field are correct like this, Check the value for Content-Type also

enter image description here

Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58