0

当我尝试通过服务器获取图像时出现此错误。
这是我通过服务器获取图像的代码:

-(void)downloadThumbnailWithHeight:(NSInteger)height width:(NSInteger)width callback:(void (^)(UIImage*))callback {
    NSString* key = [NSString stringWithFormat:@"%ldx%ld", (long)width, (long)height];
    UIImage* __block thumbnail = [_thumbnails objectForKey:key];
    if (thumbnail) { callback(thumbnail); return; }
    HttpRequest* request = [HttpRequest requestWithRelativePath:[NSString stringWithFormat:@"/api/v1/incident/%@/resize?height=%d&width=%d",self.uuid, (int)height, (int)width]];
    [HttpResponse processAsynchronousRequest:request onCompletion:^(HttpResponse* response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            thumbnail = [UIImage imageWithData:response.responseData];
            if (!thumbnail) thumbnail = [UIImage imageNamed:@"gray_thumbnail_background.png"];
            [_thumbnails setObject:thumbnail forKey:key];
            callback(thumbnail);
        });
    }];
}  

在我在 stackoverflow 中问这个问题之前,我已经尝试将它添加到我的 info.plist 中。这是我的请求:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>To Find out your location</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>To Find out your location</string>
    <key>CFBundleIdentifier</key>
    <string>com.abc.def</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundlePackageType</key>
    <string>BNDL</string>
    <key>CFBundleSignature</key>
    <string>????</string>
</dict>
</plist> 

但它仍然无法正常工作。
当我启动应用程序时,它给了我以下错误:

App Transport Security 已阻止明文 HTTP (http://) 资源加载,因为它不安全。可以通过应用程序的 Info.plist 文件配置临时例外。

请帮助我,因为我已经花了两个时间来解决这个问题。
提前致谢。

4

1 回答 1

2

如果您出于某些原因更喜欢使用NSAllowsArbitraryLoadskey 来忽略安全限制,则必须将其放在NSAppTransportSecurity字典中,因为只需将您的 dict 放在NSAppTransportSecuritykey 下。

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

但是,不建议这样做,并且可能会导致 Apple 拒绝您的应用程序。Apple 非常清楚,他们打算拒绝无特定理由使用此标志的应用程序。

更好的解决方案可能是向特定域添加异常。

参考:https ://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html

您的NSAppTransportSecurity部分可能如下所示:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
     <key>yourdomain.com</key>
     <dict>
       <key>NSIncludesSubdomains</key>
       <true/>
       <key>NSExceptionAllowsInsecureHTTPLoads</key>
       <true/>
       <key>NSExceptionMinimumTLSVersion</key>
       <string>TLSv1.2</string>
     </dict>
   </dict>
</dict>
于 2015-10-01T14:20:54.283 回答