Darshaka
Darshaka

Reputation: 189

My IOS code is not retrieving data from my json webservice

My IOS Code is

    let reposURL = NSURL(string: "http://localhost/Test/test.html")
    // 2
    do{
        if let JSONData = NSData(contentsOfURL: reposURL!) {
            // 3
            if let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
                // 4
                if let reposArray = json["items"] as? [NSDictionary] {
                    // 5
                    for item in reposArray {
                       // if let name = item.valueForKey("name") {
                        //    NameOfArtist.append(name as! String)
                        //}
                                              }
                }
            }
        }

    }catch{}

My Json Outout is

{"total_count": 741,"incomplete_results": false,"items": [{"id":47,"title":"SLAF, Navy Joint Champs at DSG 2010","main_image":"uploads/news/dsg_2010_closing_ceremony/71.jpg","modified_date":"2010-10-03 10:16:45","frontpage":"Y"},{"id":46,"title":"Defence Services Beach Volleyball","main_image":"","modified_date":"2010-09-29 14:00:02","frontpage":"Y"},{"id":45,"title":"Defence Services Squash Tournament","main_image":"","modified_date":"2010-09-29 13:55:53","frontpage":"Y"},{"id":44,"title":"Defence Services Volleyball Finals","main_image":"","modified_date":"2010-09-29 09:37:44","frontpage":"Y"},{"id":43,"title":"Defence Services Rowing Championships - 2010","main_image":"","modified_date":"2010-09-27 10:14:31","frontpage":"Y"},{"id":42,"title":"Defence Services Judo Championship","main_image":"","modified_date":"2010-09-27 06:23:30","frontpage":"Y"},{"id":41,"title":"Defence Services Weightlifting","main_image":"","modified_date":"2010-09-27 04:27:17","frontpage":"Y"},{"id":40,"title":"Defence Services Net Ball","main_image":"","modified_date":"2010-09-24 13:16:36","frontpage":"Y"},{"id":39,"title":"Defence Services Games Athletics","main_image":"uploads/news/defence_services_games_athletic/28.jpg","modified_date":"2010-09-27 09:30:32","frontpage":"Y"},{"id":38,"title":"SLAF Beats Army Men & Women In Defence Services Water Polo","main_image":"","modified_date":"2010-09-18 12:29:41","frontpage":"Y"},{"id":37,"title":"Army Emerges Defence Services Rugger Champions","main_image":"","modified_date":"2010-09-19 08:01:43","frontpage":"Y"},{"id":36,"title":"Defence Services Kabaddi Championship","main_image":"","modified_date":"2010-09-18 12:28:38","frontpage":"Y"},{"id":35,"title":"Army Defence Service Hand Ball Champions","main_image":"uploads/news/hand_ball_final/4.jpg","modified_date":"2010-09-17 09:22:54","frontpage":"Y"},{"id":34,"title":"Army & SLAF Play In The First Ever Defence Services Handball Finals","main_image":"uploads/news/defence_service_handball/1.jpg","modified_date":"2010-09-17 04:16:57","frontpage":"Y"},{"id":33,"title":"The Army Beat SLAF At Defence Services Hockey Finals","main_image":"","modified_date":"2010-09-14 04:43:52","frontpage":"Y"},{"id":32,"title":"Defence Services Games Tennis Open","main_image":"","modified_date":"2010-09-14 04:35:29","frontpage":"Y"},{"id":31,"title":"SLAF Defence Services 2010 Table Tennis Champs","main_image":"","modified_date":"2010-09-14 03:55:28","frontpage":"Y"},{"id":30,"title":"Defence Services Games 2010 - Cycling","main_image":"","modified_date":"2010-09-13 08:46:06","frontpage":"Y"},{"id":29,"title":"Navy Beat SLAF At Defence Services Rugby","main_image":"","modified_date":"2010-09-11 07:16:19","frontpage":"Y"},{"id":28,"title":"SLAF Beats The Navy At Defence Services Hockey","main_image":"","modified_date":"2010-09-11 07:15:56","frontpage":"Y"}]}

I tested this code with another web service link

"https://api.github.com/search/repositories?q=learn+swift+language:swift&sort=stars&order=desc"

..Then it works. Is there anything wrong with my code or my json file

Upvotes: 0

Views: 71

Answers (2)

Raphaël
Raphaël

Reputation: 3751

Your web service should return its response with the header Content-Type set to application/json for NSJSONSerialization.JSONObjectWithData() to work.

Upvotes: 1

techloverr
techloverr

Reputation: 2617

You have to follow this to call any web service

See Apple’s Info.plist reference for full details (thanks @gnasher729).

You can add exceptions for specific domains in your Info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>testdomain.com</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>

All the keys for each excepted domain are optional. The speaker did not elaborate on any of the keys, but I think they’re all reasonably obvious.

(Source: WWDC 2015 session 703, “Privacy and Your App”, 30:18)

You can also ignore all app transport security restrictions with a single key, if your app has a good reason to do so:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

If your app does not have a good reason, you may risk rejection:

Setting NSAllowsArbitraryLoads to true will allow it to work, but Apple was very clear in that they intend to reject apps who use this flag without a specific reason. The main reason to use NSAllowsArbitraryLoads I can think of would be user created content (link sharing, custom web browser, etc). And in this case, Apple still expects you to include exceptions that enforce the ATS for the URLs you are in control of.

If you do need access to specific URLs that are not served over TLS 1.2, you need to write specific exceptions for those domains, not use NSAllowsArbitraryLoads set to yes. You can find more info in the NSURLSesssion WWDC session.

Please be careful in sharing the NSAllowsArbitraryLoads solution. It is not the recommended fix from Apple. — kcharwood (thanks @marco-tolman)

Upvotes: 1

Related Questions