在运动中https://github.com/Mairoslav/5networkAndGDC/tree/main/ImageRequest https://github.com/Mairoslav/5networkAndGDC/tree/main/ImageRequest我想通过调整 Info.plist 中的 NSAppTransportSecurity 来允许使用“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://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://cocoacasts.com/how-to-add-app-transport-security-exception-domains and https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity
这是我的测试代码和Info.plist
,这对我来说效果很好。在真实设备上测试,而不是预览。
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()
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)