2016-11-28 25 views
0

由於蘋果將迫使我們從2017年開始啓用ATS,因此我正在進行一些調查工作,但對SDWebImage課程有一些疑問。爲什麼sdwebimage可以在App Transport Security Setting啓用時成功加載圖片?

如果我使用下面的代碼,圖像將被成功加載

[self.myImageView sd_setImageWithURL:[NSURL URLWithString:@"http://f.hiphotos.baidu.com/image/h%3D200/sign=1c3f18c4524e9258b93481eeac83d1d1/b7fd5266d0160924be0452bbd00735fae6cd3468.jpg"]]; 

但是,如果我使用的方法在NSData,圖像將不會被加載。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://f.hiphotos.baidu.com/image/h%3D200/sign=1c3f18c4524e9258b93481eeac83d1d1/b7fd5266d0160924be0452bbd00735fae6cd3468.jpg"]]; 

    UIImage *image = [UIImage imageWithData:data]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     self.myImageView.image = image; 
    }); 
}); 

SDWebImage是怎麼做到的?

回答

1

確保在您的Info.plist文件中未設置「NSAllowsArbitraryLoads」。

據我所知,SDWebImage受到所有其他應用程序相同的ATS限制。例如,當我將URL插入SDWebImage的演示應用程序(in their MasterViewController.m文件)並關閉ATS例外時,我在Xcode控制檯中收到「SDWebImage iOS Demo[6714:130852] 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.」消息。

+0

您說得對,SDWebImage受到相同的ATS限制。我終於找到了原因,當我第一次測試http鏡像加載時,我關閉了ATS,因此SDWebImage可以成功加載鏡像,但是之後我打開了ATS,SDWebImage仍然可以成功加載鏡像,但這是因爲SDWebImage緩存圖像,而不是從網絡加載,因此如果我在ATS打開時將參數更改爲另一個http圖像url,它仍然無法加載。 –

0

不太可能,但也許在加載失敗的情況下,SDWebImage會查看NSURL方案,如果它是@「http:」,則會將其更改爲@「https:」?

NSString *absoluteString = url.absoluteString; 
if ([absoluteString hasPrefix:@"http:"]) { 
    url = [NSURL URLWithString:[@"https:" stringByAppendingString:[absoluteString substringFromIndex:5]]]; 
} 
0

可能從緩存中獲取圖像,因爲這是sdwebimage在將請求發送到url之前檢查的第一個位置。

相關問題