【问题标题】:Keep getting nil instead of JSON file using dataWithContentsOfURL:使用 dataWithContentsOfURL 继续获取 nil 而不是 JSON 文件:
【发布时间】:2016-04-13 18:58:37
【问题描述】:
NSError* error = nil;

//  load JSON file from the web using url:
NSURL *internetPath = [NSURL URLWithString:url];

NSData *JSONData = [NSData dataWithContentsOfURL:internetPath options:NSDataReadingMappedIfSafe error:&error];

if (!JSONData) {
    NSLog(@"Error = %@", error);
}

以上返回如下错误:

Error = Error Domain=NSCocoaErrorDomain Code=256 "The file “antimicrobials.json” couldn’t be opened." UserInfo={NSURL=http://spectrum-prod.herokuapp.com/antimicrobials.json}

在上述错误之前,我还在控制台中收到以下错误:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

我还像这样在我的 pList 中排除了 url:

    <dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>BNDL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>NSAppTransportSecurity</key>
    <key>NSExceptionDomains</key>
    <dict>
        <key>spectrum-prod.herokuapp.com</key>
        <dict>
            <!--Include to allow subdomains-->
            <key>NSIncludesSubdomains</key>
            <true/>
            <!--Include to allow HTTP requests-->
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <!--Include to specify minimum TLS version-->
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>

</dict>

这曾经工作得很好,但现在正在引起悲伤。

有什么想法吗?

【问题讨论】:

  • 是这些错别字还是真的返回了什么:&lt;key&gt;http:/thedomain.com&lt;/key&gt;: 应该是http://,那么您发布的错误消息包含http://thedomain/data.json,没有“.com”。
  • 我认为我需要将网址设置为:“thedomain.com”(假网址)以允许与该站点的任何连接。
  • 是的,thedomian 没有 .com 只是我用错字掩盖了真实的网址 =)
  • 关键元素中缺少的 / 也是错字吗?
  • 您应该添加NSAllowsArbitraryLoads 而不是添加 domain.com 以允许所有类型的 URL。像这样&lt;key&gt;NSAppTransportSecurity&lt;/key&gt;&lt;dict&gt; &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt; &lt;true/&gt; &lt;/dict&gt;

标签: ios json nsurl


【解决方案1】:

有几个键似乎是错误的。 NSExceptionAllowsInsecureHTTPLoads 应该是 NSTemporaryExceptionAllowsInsecureHTTPLoadsNSExceptionMinimumTLSVersion 应该是 NSTemporaryExceptionMinimumTLSVersion。 试试这个:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>thedomain.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

信用: https://stackoverflow.com/a/31254874/400552

更新: nscurl --ats-diagnostics https://spectrum-prod.herokuapp.com/ 似乎暗示一切都已经过去了。所以,不确定你是否需要这些。

更新 2: 您还可以将详细的网络日志记录添加到应用程序方案的环境变量中:CFNETWORK_DIAGNOSTICS: 3

这将在控制台中打印出文件的路径。在此日志文件中,您将找到有关所发出的每个请求以及是否导致错误的大量详细信息。

查看http://www.nsscreencast.com/episodes/188-app-transport-security

更新 3: 使用NSURLSession,示例将如下所示:

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:@"https://spectrum-prod.herokuapp.com/antimicrobials.json"]
          completionHandler:^(NSData *data,
                              NSURLResponse *response,
                              NSError *error) {
            // handle response
 
  }] resume];

NSURLSession 的教程可以在这里找到:http://www.raywenderlich.com/51127/nsurlsession-tutorial

【讨论】:

  • 我编辑了我的问题以显示带有实际 url 的 plist
  • 您确定在 URL 中使用 https 吗?
  • 出于好奇,我想知道这个“临时”中缀:我搜索了苹果文档,但关键 NSTemporaryExceptionAllowsInsecureHTTPLoads 显示没有命中。您有这些“临时”密钥的官方来源吗?此处没有临时记录密钥developer.apple.com/library/ios/documentation/General/Reference/…
  • @thst 我只是从流行的堆栈溢出答案中复制/粘贴。我有一个链接到答案的信用。老实说,我没有检查苹果文档。
  • 我看到了,但他们也没有提供合适的苹果文档页面。这就是我问你的原因,但是谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 2020-09-19
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多