Alamofire 不断被取消
Posted
技术标签:
【中文标题】Alamofire 不断被取消【英文标题】:Alamofire keeps getting cancelled 【发布时间】:2017-02-05 13:45:57 【问题描述】: @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate
var window: UIWindow?
var manager1: SessionManager?
var manager2: SessionManager?
let commons:Commons = Commons()
var selected = 0
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
return true
func getDocumentsDirectory() -> URL
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
func uploade()
if(selected == 0)
let realm = try! Realm()
let uploadAuditInfo = realm.objects(UploadAudit.self)
var uploadAudit = [UploadAudit]()
let uploadDistInfo = realm.objects(UploadData.self)
var uploadDist = [UploadData]()
for i in 0 ..< uploadAuditInfo.count
if let result = uploadAuditInfo[i] as? UploadAudit
uploadAudit.append(result)
for i in 0 ..< uploadDistInfo.count
if let result = uploadDistInfo[i] as? UploadData
uploadDist.append(result)
if(uploadAudit.count > 0)
selected = 1
let upload = uploadAudit[0]
let fileNameToDelete = upload.fileName
var filePath = ""
let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs.count > 0
let dir = dirs[0] //documents directory
filePath = dir.appendingFormat("/" + fileNameToDelete)
else
return
let lang = self.commons.getLanguage()
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath)
let image = UIImage(contentsOfFile: filePath)
let ids = upload.id
let params = upload.params
let name1 = upload.fileName
let url = self.commons.getUrl()
let vas1 = lang+params
let vas2 = url+"rest/secure/"+String(ids)+"/submit?language="
let vas = vas2+vas1
print("upload audit111")
uploadFile(fileName: name1, mimeType:"application/octed-stream", image: image!, url:vas, id: ids)
else
try! realm.write
realm.delete(upload)
selected = 0
self.uploade()
else if(uploadDist.count > 0)
selected = 1
let upload = uploadDist[0]
let fileNameToDelete = upload.fileName
var filePath = ""
let dirs : [String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
if dirs.count > 0
let dir = dirs[0] //documents directory
filePath = dir.appendingFormat("/" + fileNameToDelete)
else
return
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath)
let lang = self.commons.getLanguage()
let image = UIImage(contentsOfFile: filePath)
let ids = upload.id
let params = upload.params
let name1 = upload.fileName
let url = self.commons.getUrl()
let vas1 = lang+params
let vas2 = url+"rest/secure/another/"+String(ids)+"/submit?language="
let vas = vas2+vas1
print("upload dist111")
uploadFileDist(fileName: name1, mimeType:"application/octed-stream", image: image!, url:vas, id: ids)
else
try! realm.write
realm.delete(upload)
selected = 0
self.uploade()
else
else
func uploadFileDist(fileName: String, mimeType: String, image: UIImage, url: String, id: Int )
let preferences = UserDefaults.standard
//NSUserDefaults.standardUserDefaults()
let currentLevelKey = "sessionId"
if UserDefaults.standard.object(forKey: currentLevelKey) == nil
else
let name = preferences.string(forKey: currentLevelKey)
DispatchQueue.global().async
let sessionName = "JSESSIONID="+name!
// get headers
let headers = self.commons.getHeader(cookie: sessionName)
// get language
// get configuration
let configuration = self.commons.getConfiguration(timeout: 600)
self.manager2 = Alamofire.SessionManager(configuration: configuration)
// get base url
let image_data = UIImageJPEGRepresentation(image, 0.4)
let uuid = UUID().uuidString
let uuid2 = UUID().uuidString
let nameOfImage = "profile-"+uuid+"--"+uuid2
print("dist upload")
self.manager2!.upload(multipartFormData: (MultipartFormData) in
MultipartFormData.append(image_data!, withName: "uploadFile", fileName: nameOfImage+".jpeg", mimeType: mimeType)
, to: url, method: .post, headers: headers, encodingCompletion: (result) in
//self.manager1!.session.invalidateAndCancel()
print("dist result")
switch result
case .success( let upload , _, _):
upload.responseObject(keyPath: "") (response: DataResponse<User>) in
let realm = try! Realm()
let ps = "id == "+String(id)
let datas = realm.objects(UploadData.self).filter(ps)
let datas2 = realm.objects(DistRealM.self).filter(ps)
try! realm.write
realm.delete(datas)
realm.delete(datas2)
self.selected = 0
self.uploade()
case .failure( _):
self.selected = 0
)
func uploadFile(fileName: String, mimeType: String, image: UIImage, url: String, id: Int )
let preferences = UserDefaults.standard
//NSUserDefaults.standardUserDefaults()
let currentLevelKey = "sessionId"
if UserDefaults.standard.object(forKey: currentLevelKey) == nil
else
let name = preferences.string(forKey: currentLevelKey)
DispatchQueue.global().async
// set session cookie
let sessionName = "JSESSIONID="+name!
// get headers
let headers = self.commons.getHeader(cookie: sessionName)
// get language
// get configuration
let configuration = self.commons.getConfiguration(timeout: 600)
self.manager2 = Alamofire.SessionManager(configuration: configuration)
// get base url
print(" audit upload ")
let image_data = UIImageJPEGRepresentation(image, 0.4)
let uuid = UUID().uuidString
let uuid2 = UUID().uuidString
let nameOfImage = "profile-"+uuid+"--"+uuid2
self.manager2!.upload(multipartFormData: (MultipartFormData) in
MultipartFormData.append(image_data!, withName: "uploadFile", fileName: nameOfImage+".jpeg", mimeType: mimeType)
, to: url, method: .post, headers: headers, encodingCompletion: (result) in
// self.manager1!.session.invalidateAndCancel()
print("audit result")
switch result
case .success( let upload, _, _):
upload.responseObject(keyPath: "") (response: DataResponse<User>) in
debugPrint(response)
let realm = try! Realm()
let ps = "id == "+String(id)
let datas = realm.objects(UploadAudit.self).filter(ps)
let datas2 = realm.objects(AuditRealM.self).filter(ps)
try! realm.write
realm.delete(datas)
realm.delete(datas2)
self.selected = 0
self.uploade()
case .failure( _):
self.selected = 0
)
我在 swift 3 中使用 alamofire lirbary 执行此操作,同样的调用在其他地方完美运行。但是在这里,我不断收到 Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=NSErrorFailingURLKey=
为什么我的 url 调用总是无缘无故地被取消?同样的调用在其他地方完美运行。
【问题讨论】:
【参考方案1】:你没有说任何关于应用传输安全设置,所以我试一试:
可能发生的情况是 App Transport Security 阻止了您的请求。
在 ios 9 和 OS X v10.11 中引入的应用传输安全 (ATS) 通过要求应用使用通过 HTTPS 的安全网络连接来提高用户的安全性和隐私性。
您通过更高级别 API 进行的通信需要使用具有前向保密性的 TLS 版本 1.2 进行加密。如果您尝试建立不符合此要求的连接,则会引发错误。如果您的应用需要向不安全的域发出请求,您必须在应用的 Info.plist 文件中指定该域。
在 info.plist 文件中创建异常很容易。
添加一个NSAppTransportSecurity
键,并在下面添加一个NSExceptionDomains
的字典。然后将您的域添加为键,您无需在字符串值中写入任何内容。还要添加一个NSAllowsArbitraryLoads
密钥以允许不安全的连接
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>unsecuredomain.com</key>
<string></string>
<key>sub.unsecure.co.uk</key>
<string></string>
</dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
【讨论】:
以上是关于Alamofire 不断被取消的主要内容,如果未能解决你的问题,请参考以下文章
在 swift 中使用 Alamofire 取消 api 请求
使用 Swift 3.0 的 Alamofire 4 失败:错误域 = NSURLErrorDomain 代码 = -999 “已取消”
Swift 3.0 Alamofire 4.0 - Domain=NSURLErrorDomain Code=-999 “取消”