使用 dataWithContentsOfURL 继续获取 nil 而不是 JSON 文件:
Keep getting nil instead of JSON file using dataWithContentsOfURL:
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);
}
以上returns错误如下:
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>
这曾经工作得很好,但现在却引起了麻烦。
有什么想法吗?
有几个键好像错了。 NSExceptionAllowsInsecureHTTPLoads
应该是 NSTemporaryExceptionAllowsInsecureHTTPLoads
而 NSExceptionMinimumTLSVersion
应该是 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>
来源:
更新:
nscurl --ats-diagnostics https://spectrum-prod.herokuapp.com/
似乎暗示一切都已经过去了。所以,不确定你是否需要这些。
更新二:
您还可以将详细的网络日志记录添加到应用程序方案的环境变量中: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
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);
}
以上returns错误如下:
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>
这曾经工作得很好,但现在却引起了麻烦。
有什么想法吗?
有几个键好像错了。 NSExceptionAllowsInsecureHTTPLoads
应该是 NSTemporaryExceptionAllowsInsecureHTTPLoads
而 NSExceptionMinimumTLSVersion
应该是 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>
来源:
更新:
nscurl --ats-diagnostics https://spectrum-prod.herokuapp.com/
似乎暗示一切都已经过去了。所以,不确定你是否需要这些。
更新二: 您还可以将详细的网络日志记录添加到应用程序方案的环境变量中: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