【发布时间】: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>
这曾经工作得很好,但现在正在引起悲伤。
有什么想法吗?
【问题讨论】:
-
是这些错别字还是真的返回了什么:
<key>http:/thedomain.com</key>: 应该是http://,那么您发布的错误消息包含http://thedomain/data.json,没有“.com”。 -
我认为我需要将网址设置为:“thedomain.com”(假网址)以允许与该站点的任何连接。
-
是的,thedomian 没有 .com 只是我用错字掩盖了真实的网址 =)
-
关键元素中缺少的 / 也是错字吗?
-
您应该添加
NSAllowsArbitraryLoads而不是添加 domain.com 以允许所有类型的 URL。像这样<key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>