尝试下载文件时出现 Swift Alamofire 错误
Posted
技术标签:
【中文标题】尝试下载文件时出现 Swift Alamofire 错误【英文标题】:Swift Alamofire Error While Trying to Download File 【发布时间】:2018-09-07 21:59:34 【问题描述】:我想在 Swift 4 中通过 Alamofire 从 URL 下载文件。 我是 swift 新手,经过数小时尝试找出如何安装 Alamofire 库,现在我在代码中遇到错误。 代码如下:
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(
"http://handasaim.co.il/wp-content/uploads/2018/08/6-9.xls",
method: .get,
// parameters: parameters,
encoding: JSONEncoding.default,
headers: nil,
to: destination).downloadProgress(closure: (progress) in
//progress closure
).response(completionHandler: (DefaultDownloadResponse) in
//here you able to access the DefaultDownloadResponse
//result closure
)
我得到的错误:
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.
2018-09-08 00:51:39.326074+0300 TrollingWithRoss[24805:2369287] Cannot start load of Task <CFB04BFF-0724-4AC2-84BA-74E56FFF821B>.<1> since it does not conform to ATS policy
2018-09-08 00:51:39.326266+0300 TrollingWithRoss[24805:2369283] Task <CFB04BFF-0724-4AC2-84BA-74E56FFF821B>.<1> finished with error - code: -1022
我需要对“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>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
如果我的代码不好请纠正,我还是个新手。
谢谢,
埃雷尔
【问题讨论】:
【参考方案1】:添加您的 plist 文件并解决问题 :) “Swift 4”
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
【讨论】:
谢谢!你知道如何使用这个文件(它在哪里下载的?) 现在是最难的部分,解析和显示表格中的数据。你可以找到相关的例子。 您应该只允许来自您实际需要的 URL 的不安全加载,而不是全部。以上是关于尝试下载文件时出现 Swift Alamofire 错误的主要内容,如果未能解决你的问题,请参考以下文章
使用 Alamofire 和 PromiseKit 使用 Swift 时出现 Void 错误(请求)
如何使用 Swift 5 在 Xcode 10.2.1 中添加 alamofire
无法从 Alamofire 的 Swift 3 中的 Web 服务的 PATCH API 调用中获得响应