Swift网络请求库Alamofire
发布时间:2020-12-14 07:02:25 所属栏目:百科 来源:网络整理
导读:Alamofire由cnoon大神编写的基于swift的网络请求库 Github下载地址 [TOC] 运行限制: iOS 8.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+ Xcode 7.3+ CocoaPods安装: 1.下载CocoaPods $ gem install cocoapods CocoaPods 0.39.0+ is required to build Al
Alamofire由cnoon大神编写的基于swift的网络请求库 运行限制:
CocoaPods安装:1.下载CocoaPods
CocoaPods 0.39.0+ is required to build Alamofire 3.0.0+. source 'https://github.com/CocoaPods/Specs.git'
platform :ios,'9.0'
use_frameworks!
pod 'Alamofire','~> 3.3'
3.下载:
使用Alamofire发出请求
响应处理Alamofire.request(.GET,url,parameters: ["key": "value"])
.responseJSON { response in
print(response.request)
print(response.response)
print(response.data)
print(response.result)
if let JSON = response.result.value {
print("JSON: (JSON)")
}
}
响应JSON处理Alamofire.request(.GET,url)
.responseJSON { response in
debugPrint(response)
}
HTTP方法public enum Method: String { case OPTIONS,GET,HEAD,POST,PUT,PATCH,DELETE,TRACE,CONNECT }
上传文件let fileURL = NSBundle.mainBundle().URLForResource("Default",withExtension: "png")
Alamofire.upload(.POST,file: fileURL)
上传进度Alamofire.upload(.POST,file: fileURL)
.progress { bytesWritten,totalBytesWritten,totalBytesExpectedToWrite in
print(totalBytesWritten)
// This closure is NOT called on the main queue for performance
// reasons. To update your ui,dispatch to the main queue.
dispatch_async(dispatch_get_main_queue()) {
print("Total bytes written on main queue: (totalBytesWritten)")
}
}
.responseJSON { response in
debugPrint(response)
}
下载Alamofire.download(.GET,url) { temporaryURL,response in
let fileManager = NSFileManager.defaultManager()
let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory,inDomains: .UserDomainMask)[0]
let pathComponent = response.suggestedFilename
return directoryURL.URLByAppendingPathComponent(pathComponent!)
}
HTTP头部Alamofire.request(.GET,headers: ["key":"value"])
.responseJSON { response in
debugPrint(response)
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |