在练习https://github.com/Mairoslav/5networkAndGDC/tree/main/ImageRequest中,我希望通过调整NSAppTransportSecurity中的Info.plist (如下面所写),允许从网站上加载带有"http“方案的jpg。我不想允许任何其他"http“站点,因此选择将NSAllowsArbitraryLoads设置为false。如果你能提供建议的话。
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<false/>
<key>NSExceptionDomains</key>
<dict>
<key>www.kittenswhiskers.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>即使在这些调整之后,也有一个错误:“无法加载资源,因为策略需要使用安全连接。”我已经尝试了NSExceptionMinimumTLSVersion的所有备选方案。我使用的来源是:https://agilie.com/blog/how-to-add-domain-exceptions-to-comply-with-apples-new-ats-requirements、https://cocoacasts.com/how-to-add-app-transport-security-exception-domains和https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity
发布于 2022-09-25 23:38:02
这是我的测试代码和Info.plist,这对我来说很好。在真实的设备上测试,而不是预览。
The 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>
<false/>
<key>NSExceptionDomains</key>
<dict>
<key>www.kittenswhiskers.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
</dict>
</plist>测试代码
struct ContentView: View {
@State var image = UIImage()
var body: some View {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 333, height: 333)
.cornerRadius(10)
.task {
if let url = URL(string: "http://www.kittenswhiskers.com/wp-content/uploads/sites/23/2014/02/Kitten-playing-with-yarn.jpg") {
image = await loadImage(url: url)
}
}
}
func loadImage(url: URL) async -> UIImage {
do {
let (data, _) = try await URLSession.shared.data(from: url)
if let img = UIImage(data: data) { return img }
}
catch { print(error) }
return UIImage()
}
}https://stackoverflow.com/questions/73847451
复制相似问题