2

I am trying to fetch JSON from a URL but cannot do so because an error is occuring and therefore response and data are nil.

My code for getting data from URL

 loadData(urlString: String, completion : @escaping (Player,Player) -> Void) -> Void {
    let config = URLSessionConfiguration.default

    let session = URLSession(configuration: config)

    let url = URL(string: urlString)
    let task = session.dataTask(with: url!){data,response,error in
        if error != nil{
            print("problem in loading data");
            print(error.debugDescription);
        } else{
            let players = self.parseJson(data: data as NSData?)

            let priority = DispatchQueue.global(qos: .userInitiated)
            priority.async {
                DispatchQueue.main.async {
                    completion(players.0, players.1);
                }
            }
        }
    }
    task.resume();

And the Debug description of the error is as follows with the details of App Transport Security in info.plist - Image showing info.plist and error.debugDescription

And the image of code I a using for getting the data from JSON URL. enter image description here

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
user3745635
  • 847
  • 1
  • 8
  • 21
  • Something is wrong with your server. You should add more specific keywords related to `SSL`, `SSH` and `server`. I have many apps (even on store) which are interacting directly to IP address without any issue. – Blind Ninja Nov 23 '16 at 12:07
  • @HarvantS. Can you please tell me how to do it? – user3745635 Nov 23 '16 at 12:11
  • He probably can't because it's unclear what the problem is. You will need to look at the certificate your server is sending. Maybe open a page on the server in a browser and see whether the browser has anything to say about the certificate – Pekka Nov 23 '16 at 12:20
  • I am totaly agree with @Pekka웃. – Blind Ninja Nov 23 '16 at 12:28
  • @Pekka웃 , I don't have any issues in opening the URL in my browser it is showing JSON without any error or problem. – user3745635 Nov 23 '16 at 12:29
  • Are you accessing it through HTTPS in your browser? – Pekka Nov 23 '16 at 12:30
  • @Pekka웃 , yes I am accessing through HTTPS in my browser and it is working fine. but I also tried with HTTP and when I do so it gives 404 Not Found The requested URL /test.json was not found on this server. – user3745635 Nov 23 '16 at 12:36
  • @HarvantS. , https://test.wowjust.watch/test.json is teh URL can you help me out. – user3745635 Nov 23 '16 at 12:44
  • @Pekka웃 , https://test.wowjust.watch/test.json is the URL can you help me out now? – user3745635 Nov 23 '16 at 12:45
  • Site does not have valid certificate. Ask your server admin for it. Site is unsafe for browsing. Even google chrome is not allowing me to enter. – Blind Ninja Nov 23 '16 at 12:47
  • @HarvantS. , I am appyling for the intern and I told this to the company and they told me that this is possible and this is the part of the problem. I am struggling for days on this. – user3745635 Nov 23 '16 at 12:49
  • Ask them to open the URL and check the certificate status via `Inspect Element`. – Blind Ninja Nov 23 '16 at 12:50
  • When I open the site in the browser I'm getting a certificate warning in Safari. It appears the certificate is issued for `dev2.wowjust.watch`. You would probably need a certificate that is valid for the `test.` host name. – Pekka Nov 23 '16 at 13:20
  • so, do you have any idea @Pekka웃 what can be done on my hand because I don't have access to anything that is server side. – user3745635 Nov 23 '16 at 13:27
  • Getting iOS apps to ignore certificate warnings and such is difficult, and they get stricter with every version. Not sure what the current status is. Here is some discussion http://stackoverflow.com/questions/32876602/swift-2-xcode-7-0-cannot-access-https-site-with-unstrusted-ssl-certificate then maybe you can use a non-https connection for the time being? That requires a special setting in infoplist and is possible only until the end of this year, though, if I remember correctly. – Pekka Nov 23 '16 at 13:32

1 Answers1

0

Use NSURLSessionDelegate

enter image description here

You need to add proper session configuration

enter image description here Implement - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler

enter image description here

Imran
  • 3,045
  • 2
  • 22
  • 33
  • 1
    This handler just accepts any server certificate. Maybe that's fine for testing, but otherwise it's a security issue. – Mikkel K. Apr 21 '20 at 10:47