首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过在ATS中添加NSExceptionDomains从http加载png?

如何通过在ATS中添加NSExceptionDomains从http加载png?
EN

Stack Overflow用户
提问于 2022-09-25 19:53:58
回答 1查看 73关注 0票数 1

在练习https://github.com/Mairoslav/5networkAndGDC/tree/main/ImageRequest中,我希望通过调整NSAppTransportSecurity中的Info.plist (如下面所写),允许从网站上加载带有"http“方案的jpg。我不想允许任何其他"http“站点,因此选择将NSAllowsArbitraryLoads设置为false。如果你能提供建议的话。

代码语言:javascript
复制
<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-requirementshttps://cocoacasts.com/how-to-add-app-transport-security-exception-domainshttps://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-25 23:38:02

这是我的测试代码和Info.plist,这对我来说很好。在真实的设备上测试,而不是预览。

The Info.plist

代码语言:javascript
复制
 <?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>

测试代码

代码语言:javascript
复制
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()
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73847451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档