I'm trying to make a https request to my website api and I'm been working on it for 2days but didn't find a solution to all my needs (it's in iOS 10). My https certificate is unsigned (https://api.example.de/v1/something) with a token as 'Authorization' header. then I want to download the JSON file and parse it. here's my code:
func testDownload(token: String){
let url = URL(string: "https://api.example.de/v1/blabla")
var request = URLRequest(url: url!)
request.httpMethod = "GET"
request.addValue("Token \(token)", forHTTPHeaderField: "Authorization")
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
do{
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: AnyObject]
let parsed_data = json["data"] as? [[String: AnyObject]] ?? []
let firstName = parsed_data[0]["firstName"] as? String
print("Firstname for first item is: \(firstName)")
self.employeeName.text = firstName
} catch let error as NSError{
print("Sorry there is an Error: \(error)")
}
})
task.resume()
I'm getting this error which seems unrelated:
2017-03-03 10:16:52.636 44 App[1763:34479] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9843)
fatal error: unexpectedly found nil while unwrapping an Optional value
btw, I have already changed the info.plist like This Screenshot and also have tested this code on two different APIs (one with a certified SSL and one with a normal HTTP protocol) and parsed the data.
What I'm doing wrong? please help me
The problem was only the certificate, if a HTTPS certificate is unsigned then there is no way to make an HTTP request. If it is HTTPS then the certificate must be signed.
The problem was solved as soon as we signed our certificate.