我想加载这个jpeg:https://i.groupme.com/638x640.jpeg.d4f31c747b534baca03d12db5a2b6193
但我得到了错误 The operation couldn’t be completed. (NSURLErrorDomain error -1100.)
从这篇文章中,我发现错误意味着:kCFURLErrorFileDoesNotExist.这很奇怪,因为那里肯定有一个图像.
从这篇文章来看,问题似乎与https协议有关,但在我实现解决方案切换到之后http,我得到了错误:
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. ... The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
在进行必要的plist更改后,同样的错误会继续弹出:
如何在iOS下载此图像?
UPDATE
所以https如果我使用这段代码,我发现下载确定:
NSData* theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://i.groupme.com/638x640.jpeg.d4f31c747b534baca03d12db5a2b6193"]];
UIImage *image = [UIImage imageWithData:theData];
Run Code Online (Sandbox Code Playgroud)
但如果我使用这个库它不起作用:https://github.com/rs/SDWebImage
似乎没有其他https人和那个库有问题,所以我认为它仍然是我的设置问题?
您提供的域名错误.
删除您手动添加的所有plist设置,将其添加到plist中----
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>i.groupme.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>
Run Code Online (Sandbox Code Playgroud)
Plist截图
NSURL *url = [[NSURL alloc] initWithString:@"https://i.groupme.com/638x640.jpeg.d4f31c747b534baca03d12db5a2b6193"];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *result = [[UIImage alloc] initWithData:data];
Run Code Online (Sandbox Code Playgroud)