当前位置:网站首页>Packaging and use of alamofire framework
Packaging and use of alamofire framework
2022-07-23 16:09:00 【Hanyang Li】
1. Alamofire github Address : Alamofire
2. The terminal switches to the project folder , Execution instruction pod init , In the project folder , open Podfile file , Write stock in name
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target ' test -01-Alamofire' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for test -01-Alamofire
pod 'Alamofire'
end3. Back to the terminal , Input instruction pod search Alamofire -> pod help -> pod install --no-repo-update, Download finished Library
4. Test request method
/// Network request
/// - Parameters:
/// - method: GET / POST
/// - URLString: URLString
/// - parameters: Parameter Dictionary
/// - finished: Complete callback
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. Test the method of uploading files
/// Upload files
private func upload(URLString: String, data: Data, name: String, parameters: [String: Any]?, finished: @escaping requestCallBack){
var parameter = parameters
// If you add token Failure , Go straight back to
if !appendToken(parameters: ¶meter){
// token Invalid
finished(nil,NSError(domain: "cn.cast.error", code: -1001, userInfo: ["message":"token It's empty "]))
return
}
/**
1. data To upload the binary of the file
2. name Is the field name defined by the server - The background interface document will prompt
3. fileName Is the file name saved in the server , however : Now you can usually write casually , The background will do some processing
- According to the uploaded file , Generate thumbnail , Medium map , High definition pictures
- Save in different paths , And automatically generate the file name
- fileName yes HTTP Properties defined by the Protocol
4. mimeType / contentType: The client notifies the server , Exact type of binary data
- Large type / Subtype
* image/gif image/jpg image/png
* text/plain text/html
* application/json
- Inaccurate type of server
* 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> Upload files
AF.upload(multipartFormData: { multipartFormData in
// Splice the binary data of the uploaded file
multipartFormData.append(data, withName: name, fileName: "test", mimeType: "application/octet-stream")
// Traverse the parameter dictionary , Generate corresponding parameter data
if let parameter = parameter {
for (k, v) in parameter {
let str = v as! String
let strData = str.data(using: .utf8)!
// data yes v Binary data of name yes K
multipartFormData.append(strData, withName: k)
}
}
}, to: URLString)
.responseJSON { response in
print(response.result)
switch response.result{
case let .success(data):
// Complete callback
finished(data,nil)
break
case let .failure(error):
// When developing network applications , Don't prompt the user for errors , But errors must be output
print(error)
finished(nil,error)
break
}
}
}边栏推荐
- How to become an elegant Hardware Engineer?
- Redis master-slave replication
- 剑指 Offer II 115. 重建序列 : 拓扑排序构造题
- MySQL-字符串按照数值排序
- ESP8266 NodeMCU 闪存文件系统(SPIFFS)
- Exclusive interview | open source Summer Star Niu Xuewei
- 链表合并(暑假每日一题 3)
- One minute rule for sequential disk access
- Ultra detailed MP4 format analysis
- C语言学习笔记
猜你喜欢

後綴錶達式(暑假每日一題 4)

xml-xxe漏洞之Fake XML cookbook

Mysql—六大日志

【攻防世界WEB】难度三星9分入门题(下):shrine、lottery

【攻防世界WEB】难度三星9分入门题(中):ics-05、easytornado

(Zset) how is the underlying layer of redis stored with a hop table

【攻防世界WEB】难度三星9分入门题(上):simple_js、mfw

Software testing weekly (No. 81): what can resist negativity is not positivity, but concentration; What can resist anxiety is not comfort, but concrete.

【论文学习】《Source Mixing and Separation Robust Audio Steganography》

Dark horse programmer - interface test - four day learning interface test - third day - advanced usage of postman, export and import of Newman case set, common assertions, assertion JSON data, working
随机推荐
Day14 function module
[attack and defense world web] difficulty Samsung 9-point introductory question (middle): ics-05, easytornado
【攻防世界WEB】难度三星9分入门题(中):ics-05、easytornado
Design idea of initializing page input parameters
上课作业(5)——#576. 饥饿的牛(hunger)
Safe and reasonable use of electricity to harvest a cool "summer"
Suffix expression (summer vacation daily question 4)
Custom encapsulation pop-up box (with progress bar)
C # close current computer command
TranslucentTB 推荐
PHP code audit 4 - SQL injection vulnerability
Axure advanced
任务切换的细节
2022 the most NB JVM foundation to tuning notes, thoroughly understand Alibaba P6 small case
【论文学习】《Source Mixing and Separation Robust Audio Steganography》
Summary of server performance tuning experience
lc marathon 7.23
《快速掌握QML》第五章 组件
Harbor image warehouse
JS filter / replace sensitive characters