我的 IOS 代码没有从我的 json 网络服务中检索数据



我的IOS代码是

    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{}

我的 Json Outout 是

{"total_count": 741,"incomplete_results": 假,"项目": [{"id":47,"title":"SLAF, 海军联合冠军在 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":"国防服务沙滩排球","main_image":","modified_date":"

2010-09-29 14:00:02","frontpage":"Y"},{"id":45,"title":"国防壁球锦标赛","main_image":","modified_date":"2010-09-29 13:55:53","首页":"Y"},{"id":44,"title":"国防服务排球决赛","main_image":","modified_date":"2010-09-29 09:37:44","首页":"Y"},{"id":43,"title":"国防服务赛艇锦标赛 - 2010","main_image":","modified_date":"2010-09-27 10:14:31","frontpage":"Y"},{"id":42,"title":"国防柔道锦标赛","main_image":","modified_date":"2010-09-27 06:23:30","frontpage":"Y"},{"id":41,"title":"国防服务举重","main_image":","modified_date":"2010-09-27 04:27:17","frontpage":"Y"},{"id":40,"title":"国防服务网球","main_image":","modified_date":"2010-09-24 13:16:36","frontpage":"Y"},{"id":39,"title":"国防服务运动会田径","main_image":"uploads/news/defence_services_games_athletic/28.jpg","modified_date":"2010-09-27 09:30:32","frontpage":"Y"},{"id":38,"title":"SLAF在国防服务用水中击败陆军男女Polo","main_image":","modified_date":"2010-09-18 12:29:41","frontpage":"Y"},{"id":37,"title":"陆军崛起国防服务橄榄球冠军","main_image":","modified_date":"2010-09-19 08:01:43","frontpage":"Y"},{"id":36,"title":"国防服务卡巴迪锦标赛","main_image":","modified_date":"2010-09-18 12:28:38","frontpage":"Y"},{"id":35,"title":"陆军国防服务手球冠军","main_image":"uploads/news/hand_ball_final/4.jpg","modified_date":"2010-09-17 09:22:54","frontpage":"Y"},{"id":34,"title":"陆军和SLAF参加有史以来第一届国防服务手球决赛","main_image":"uploads/news/defence_service_handball/1.jpg","modified_date":"2010-09-17 04:16:57","frontpage":"Y"},{"id":33,"title":"陆军在国防服务曲棍球决赛中击败SLAF","main_image":","modified_date":"2010-09-14 04:43:52","frontpage":"Y"},{"id":32,"title":"国防服务运动会网球公开赛","main_image":","modified_date":"2010-09-14 04:35:29","frontpage":"Y"},{"id":31,"title":"SLAF 国防服务2010年乒乓球锦标赛","main_image":","modified_date":"2010-09-14 03:55:28","frontpage":":"Y"},{"id":30,"title":"国防服务运动会2010 - 自行车","main_image":","modified_date":"2010-09-13 08:46:06","frontpage":"Y"},{"id":29,"title":"海军在国防服务橄榄球比赛中击败SLAF","main_image":","modified_date":"2010-09-11 07:16:19","frontpage":"Y"},{"id":28,"title":"SLAF在国防服务曲棍球上击败海军","main_image":","modified_date":"2010-09-11 07:15:56","frontpage":"Y"}]}

我用另一个 Web 服务链接测试了这段代码

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

..然后它起作用了。我的代码或 json 文件有什么问题吗

您必须遵循此操作才能调用任何 Web 服务

有关完整的详细信息,请参阅Apple的Info.plist参考(感谢@gnasher729)。

您可以在 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>

每个例外域的所有密钥都是可选的。演讲者没有详细说明任何键,但我认为它们都相当明显。

(来源:WWDC 2015 会议 703,"隐私和您的应用程序",30:18)

如果你的应用有充分的理由,还可以使用单个密钥忽略所有应用传输安全限制:

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

如果您的应用没有充分的理由,您可能会面临被拒绝的风险:

将 NSAllowsArbitraryLoads 设置为 true 将允许它工作,但是 苹果非常明确,他们打算拒绝使用它的应用程序 没有特定原因的标志。使用的主要原因 NSAllowsArbitraryLoad我能想到的是用户创建的内容 (链接共享、自定义网络浏览器等)。在这种情况下,苹果仍然 希望您包含对 URL 强制执行 ATS 的例外 一切由您掌控。

如果确实需要访问未通过 TLS 1.2 提供的特定 URL,则需要为这些域编写特定的例外,而不是使用 NSAllowsArbitraryLoads 设置为 yes。您可以在 NSURLSesssion WWDC 会话中找到更多信息。

在共享 NSAllowsArbitraryLoads 解决方案时请小心。这不是苹果推荐的修复程序。- Kcharwood(感谢@marco-Tolman)

Web 服务应返回其响应,并将标头Content-Type设置为 application/json,以便NSJSONSerialization.JSONObjectWithData()正常工作。

最新更新