当前位置:网站首页>Alamofire 框架封装与使用
Alamofire 框架封装与使用
2022-07-23 11:36:00 【Hanyang Li】
1. Alamofire github 地址: Alamofire
2. 终端切换到项目文件夹, 执行指令 pod init , 项目文件夹中, 打开 Podfile 文件,写入库名称
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target '测试-01-Alamofire' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for 测试-01-Alamofire
pod 'Alamofire'
end3. 回到终端,输入指令 pod search Alamofire -> pod help -> pod install --no-repo-update, 下载完成库
4. 测试请求方法
/// 网络请求
/// - Parameters:
/// - method: GET / POST
/// - URLString: URLString
/// - parameters: 参数字典
/// - finished: 完成回调
private func request(method: HTTPMethod, URLString: String, parameters: [String: Any]?, finished: @escaping requestCallBack){
AF.request(URLString, method: method, parameters: parameters).responseJSON{ response in
switch response.result{
case let .success(data):
finished(data,nil)
break
case let .failure(error):
finished(nil,error)
break
}
}
}5. 测试上传文件方法
///上传文件
private func upload(URLString: String, data: Data, name: String, parameters: [String: Any]?, finished: @escaping requestCallBack){
var parameter = parameters
//如果追加 token 失败,直接返回
if !appendToken(parameters: ¶meter){
// token 无效
finished(nil,NSError(domain: "cn.cast.error", code: -1001, userInfo: ["message":"token 为空"]))
return
}
/**
1. data 要上传文件的二进制
2. name 是服务器定义的字段名称 - 后台接口文档会提示
3. fileName 是保存在服务器的文件名,但是:现在通常可以随便写,后台会做一些处理
- 根据上传的文件,生成 缩略图,中等图,高清图
- 保存在不同路径,并且自动生成文件名
- fileName 是 HTTP 协议定义的属性
4. mimeType / contentType: 客户端通知服务器,二进制数据的准确类型
- 大类型 / 小类型
* image/gif image/jpg image/png
* text/plain text/html
* application/json
- 服务器不准确的类型
* application/octet-stream
*/
//TODO
// post(URLString, parameters: parameter, headers: nil, constructingBodyWith: { formData in
// formData.appendPart(withFileData: data, name: name, fileName: "test", mimeType: "application/octet-stream")
// }, progress: nil) { _, result in
// finished(result,nil)
// } failure: { _, error in
// print(error)
// finished(nil,error)
// }
//2>上传文件
AF.upload(multipartFormData: { multipartFormData in
//拼接上传文件的二进制数据
multipartFormData.append(data, withName: name, fileName: "test", mimeType: "application/octet-stream")
//遍历参数字典,生成对应的参数数据
if let parameter = parameter {
for (k, v) in parameter {
let str = v as! String
let strData = str.data(using: .utf8)!
// data 是 v 的二进制数据 name 是 K
multipartFormData.append(strData, withName: k)
}
}
}, to: URLString)
.responseJSON { response in
print(response.result)
switch response.result{
case let .success(data):
//完成回调
finished(data,nil)
break
case let .failure(error):
//在开发网络应用中的时候,错误不要提示给用户,但是错误一定要输出
print(error)
finished(nil,error)
break
}
}
}边栏推荐
- 【运维】ssh tunneling 依靠ssh的22端口实现访问远程服务器的接口服务
- How to become an elegant Hardware Engineer?
- C语言经典例题-用4×4矩阵显示从1到16的所有整数,并计算每行、每列和每条对角线上的和
- MD5 strong collision, secondary decoding,
- String and integer convert each other
- Where can I download airserver? How to use tutorial
- Day14 function module
- Bug modification
- Without Huawei, Qualcomm will raise prices at will, and domestic mobile phones that lack core technology can only be slaughtered
- xxl-job 实现email发送警告的代码解析(一行一行代码解读)
猜你喜欢

Summary of server performance tuning experience

After Effects 教程,如何在 After Effects 中创建动画?

一个悄然崛起的国产软件,太强了!

Quickly master QML Chapter 5 components

【运维】ssh tunneling 依靠ssh的22端口实现访问远程服务器的接口服务

Axure advanced

Unity notes ilruntime access

C语言经典例题-商品检验码

种种迹象表明,Apple将有望支持AV1

重磅 | CertiK:2022年第二季度Web3.0行业安全报告发布(附PDF下载链接)
随机推荐
Where can I download airserver? How to use tutorial
Opnsense - multifunctional, highly reliable and easy-to-use firewall (II)
Custom encapsulation pop-up box (with progress bar)
day14函数模块
SharedPreferences数据储存
2022最NB的JVM基础到调优笔记,吃透阿里P6小case
TranslucentTB 推荐
没有了华为,高通任意涨价,缺乏核心技术的国产手机只能任由宰割
远程系统命令执行
C语言经典例题-求最少数量钞票
3D math - vector
AWS篇1
对专利的学习
(BFS) template + example (maze, eight digits)
【论文学习】《Source Mixing and Separation Robust Audio Steganography》
xxl-job 实现email发送警告的代码解析(一行一行代码解读)
2022 the most NB JVM foundation to tuning notes, thoroughly understand Alibaba P6 small case
(Zset)Redis底层是如何用跳表进行存储的
UmiJs - qiankun主子应用之间,数据的传递
10100