-4

dears

i am using this code

var Locations = [LocationListStats]()

func downloadJSON(completed: @escaping () -> ()) {

    let url = URL(string: "http://teslm.net/app/LocationList.php")

    URLSession.shared.dataTask(with: url!) { (data, response, error) in
        do {
            self.Locations = try JSONDecoder().decode([LocationListStats].self, from: data!)        
            DispatchQueue.main.async {
                completed()
            }
        }catch {
            print("JSON Error")
        }
    }.resume()
}

but i am getting this error : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

but if i use other URL it working without any error. so do you think i have issue with my code here or i should do something with my Json file at back end ?

error here in this line:

self.Locations = try JSONDecoder().decode([LocationListStats].self, from: data!)

if i print the error it give me this:

The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

i used this in my plist file to try to solve this issue but same thing:

    <key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>http://teslm.net/app/LocationList.php</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
        </dict>
    </dict>
</dict>

i think this issue is related to HTTPS protocol am i right ?

  • 1
    Possible duplicate of [Transport security has blocked a cleartext HTTP](https://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http). Other than the fact that you clearly didn't even search the error message, there are several issues with your code. You should always check the `error` variable being `nil` and safely unwrap `data` when performing a URL request. Dispatching the completion handler call to the main thread is unnecessary and makes no sense either. Finally, in the catch block you should print the actual error. – Dávid Pásztor Jan 07 '18 at 21:47
  • i did do what you said please check my edit – mohammed khuly Jan 07 '18 at 21:52
  • guys i swayer to god i search every where for this issue i could not find any answer hopping to find it here – mohammed khuly Jan 07 '18 at 21:56
  • 1
    `http://teslm.net/app/LocationList.php` is not a domain name. – rmaddy Jan 07 '18 at 22:17

1 Answers1

0

You exception is not correct. The NSExceptionDomain should only be the domain. You also need to add an exception to allow insecure connections.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>teslm.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
        </dict>
    </dict>
</dict>

Note, Apple has stated that at some point in the future, you will have to provide justification for why you need these exceptions. If you own the teslm.net server, you should wotk to get it to support secure connections, including all the requirements for ATS.

wottle
  • 13,095
  • 4
  • 27
  • 68