์ฑ์ ๊ฐ๋ฐํ๋ค ๋ณด๋ฉด, ์๋ฒ์ ํต์ ํ๋ ๋ถ๋ถ์ด ํ์์ ์
๋๋ค. ์ด๋ด ๋, ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณตํ๋ Foundation์ URLSession์ ์ฌ์ฉํ์ฌ ๋คํธ์ํน์ ์ํํฉ๋๋ค.
ํ์ง๋ง, URLSession์ ๋ค์ ๋ฒ๊ฑฐ๋ก์ด ์ ์ด ์๊ธฐ ๋๋ฌธ์ ์คํ ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ Alamofire๋ฅผ ์ฃผ๋ก ์ฌ์ฉํฉ๋๋ค.
Alamofire
Alamofire๋ swift๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ HTTP ๋คํธ์ํน ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค. ์์์ ์ธ๊ธํ Foundation์ URLSession ๊ธฐ๋ฐ์ผ๋ก ๊ตฌํ๋์ด ์์ด, ์ด๋ฅผ ๋ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ํฉ๋๋ค.
https://github.com/Alamofire/Alamofire
GitHub - Alamofire/Alamofire: Elegant HTTP Networking in Swift
Elegant HTTP Networking in Swift. Contribute to Alamofire/Alamofire development by creating an account on GitHub.
github.com
Alamofire๋ ๋ค์ ๊ธฐ๋ฅ๋ค์ ์ ๊ณตํ๋๋ฐ, ์ด ์ธ์๋ ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๊ณ ์์ต๋๋ค.
- Chainable request/response methods
- JSON, Codable decoding
- Authentication
์์ฝํ์๋ฉด, Alamofire๋ HTTP ๋คํธ์ํน์ ํ๋๋ฐ ์์ด์ ์์ฃผ ์ฌ์ฉํ๋ ์ฝ๋ ๋ฐ ํจ์๋ฅผ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ํฉ๋๋ค.
๋คํธ์ํฌ ํต์ ์ ํ๊ธฐ ์ํด ๊ธฐ๋ณธ์ ์ผ๋ก ์์์ผ ํ๋ ๊ฒ์ด ์์ต๋๋ค.
๋ฐ๋ก Request Methods ์
๋๋ค.
Request Methods
Request Methods๋ ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ์๊ฒ ์ฌ์ฉ์ ์์ฒญ์ ๋ชฉ์ ์ด๋ ์ข
๋ฅ๋ฅผ ์๋ฆฌ๋ ์๋จ์
๋๋ค.
์ด ์ค์์๋ ์์ฃผ ์ฌ์ฉ๋๋ ๋ฉ์๋ ์์ฃผ๋ก ์ค๋ช
ํ๊ฒ ์ต๋๋ค.
HEAD
GET๊ณผ ์ ์ฌํ์ง๋ง, ์๋ต ๋ณธ๋ฌธ์ ํฌํจํ์ง ์๋๋ค๋ ์ฐจ์ด๊ฐ ์์ต๋๋ค. ๋ฐ๋ผ์ ๋ฆฌ์์ค์ ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ฑฐ๋, Content-Length ๋ฐ Last-Modified์ ๊ฐ์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์กฐํํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
๋ณธ๋ฌธ์ ์ ์กํ์ง ์๊ธฐ ๋๋ฌธ์ ๋คํธ์ํฌ ํธ๋ํฝ์ ์ ์ฝํ ์ ์๋ ์ฅ์ ์ด ์์ต๋๋ค.
HEAD /api/resource HTTP/1.1
Host: example.com
GET
์๋ฒ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ์ฉ๋๋ก ์ฌ์ฉ๋ฉ๋๋ค. ๋ฉฑ๋ฑ์ฑ์ ๊ฐ์ง๋ฏ๋ก ๋์ผํ ์์ฒญ์ ์ฌ๋ฌ ๋ฒ ์ํํ๋๋ผ๋ ์๋ฒ์ ์ํ๊ฐ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค. ๋ํ ์บ์ฑ์ด ๊ฐ๋ฅํ์ฌ ๋ธ๋ผ์ฐ์ ๋ ํ๋ก์ ์๋ฒ๊ฐ ์๋ต์ ์ ์ฅํ๊ณ ์ฌ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋ฐ์ดํฐ ์ ๋ฌ ์์๋ URL์ ์ฟผ๋ฆฌ ์คํธ๋ง์ ํ์ฉํฉ๋๋ค.
GET /api/users?id=123 HTTP/1.1
Host: example.com
POST
์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ์ฌ ์๋ก์ด ๋ฆฌ์์ค๋ฅผ ์์ฑํ๊ฑฐ๋ ํน์ ์์ ์ ์ํํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์์ฒญ ๋ณธ๋ฌธ์ ํฌํจํ ์ ์์ผ๋ฉฐ, ๋ฉฑ๋ฑ์ฑ์ ๊ฐ์ง์ง ์์ผ๋ฏ๋ก ๋์ผํ ์์ฒญ์ ์ฌ๋ฌ ๋ฒ ์ํํ๋ฉด ์๋ฒ์ ์ํ๊ฐ ๋ฌ๋ผ์ง ๊ฐ๋ฅ์ฑ์ด ์์ต๋๋ค.
์ฌ์ฉ์ ๋ฑ๋ก, ๋ก๊ทธ์ธ, ๋ฐ์ดํฐ ์ ์ถ ๋ฑ์ ์์ ์์ ์ฃผ๋ก ํ์ฉ๋ฉ๋๋ค.
POST /api/users HTTP/1.1
Host: example.com
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com"
}
PUT
์ง์ ๋ ๋ฆฌ์์ค๋ฅผ ์์ฑํ๊ฑฐ๋ ์ ์ฒด์ ์ผ๋ก ์์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. GET๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฉฑ๋ฑ์ฑ์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ๋์ผํ ์์ฒญ์ ์ฌ๋ฌ ๋ฒ ์ํํ๋๋ผ๋ ๊ฒฐ๊ณผ๊ฐ ๋ณํ์ง ์์ต๋๋ค.
๊ธฐ์กด ๋ฆฌ์์ค๊ฐ ์กด์ฌํ๋ฉด ๋ฎ์ด์ฐ๊ธฐ๊ฐ ์ด๋ฃจ์ด์ง๋ฉฐ, ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ์๋ก์ด ๋ฆฌ์์ค๊ฐ ์์ฑ๋ ์๋ ์์ต๋๋ค. ์์ฒญ ๋ณธ๋ฌธ์๋ ๋ฆฌ์์ค์ ์ ์ฒด ์ ๋ณด๊ฐ ํฌํจ๋์ด์ผ ํ๋ฉฐ, ๋ถ๋ถ์ ์ธ ์ ๋ฐ์ดํธ๋ณด๋ค๋ ์ ์ฒด ๊ฐฑ์ ์ด ํ์ํ ๋ ์ ํฉํฉ๋๋ค.
PUT /api/users/123 HTTP/1.1
Host: example.com
Content-Type: application/json
{
"name": "Jane Doe",
"email": "jane@example.com"
}
DELETE
์๋ฒ์์ ํน์ ๋ฆฌ์์ค๋ฅผ ์ญ์ ํ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค. ๋ฉฑ๋ฑ์ฑ์ ๊ฐ์ง๋ฏ๋ก ๋์ผํ ์์ฒญ์ ๋ฐ๋ณตํ๋๋ผ๋ ๊ฒฐ๊ณผ๋ ๋์ผํ๊ฒ ์ ์ง๋ฉ๋๋ค.
์๋ฒ์ ์ ์ฑ ์ ๋ฐ๋ผ ์์ฒญ ์ฆ์ ์ญ์ ๋๊ฑฐ๋ ์ผ์ ๊ธฐ๊ฐ ๋ณด๊ด ํ ์ญ์ ๋ ์๋ ์์ผ๋ฉฐ, ์ผ๋ถ ๊ตฌํ์์๋ ์ญ์ ์์ฒญ์ ๋ฐ์ ํ ๋นํ์ฑํ ์ฒ๋ฆฌํ๋ ๋ฐฉ์๋ ์กด์ฌํฉ๋๋ค.
DELETE /api/users/123 HTTP/1.1
Host: example.com
Request/Reponse
์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก API ์์ฒญ ์ฝ๋๋ฅผ ๊ตฌํํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
GET
let url = "https://test.com"
AF.request(
url,
method: .get,
parameters: nil,
encoding: URLEncoding.default,
headers: ["Content-Type":"application/json", "Accept":"application/json"]
).validate(statusCode: 200..<300)
.responseJSON { response in
/** ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ ํ์ฉ */
switch response.result {
case .success(let data):
/** ์ ์์ ์ผ๋ก reponse๋ฅผ ๋ฐ์ ๊ฒฝ์ฐ */
case .failure(let error):
/** ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ */
}
}
- url : ์์ฒญํ url
- method : ์ด๋ค request method๋ฅผ ์ฌ์ฉํ ๊ฒ์ธ์ง ์์ฑ
- parameters : request body๋ฅผ ์ฌ์ฉํ ๋ ์ ๋ฌํ ๊ฐ (GET์ ์ฌ์ฉํ ๊ฒ์ด๋ฏ๋ก nil)
- encoding : ์ธ์ฝ๋ฉ ๋ฐฉ์
- headers : ๋ถ๊ฐ์ ์ธ ์ ๋ณด๋ฅผ ์๋ฏธ (์ฝ๋์์๋ ์ก/์์ ๋ฐ์ดํฐ ํ์ (JSON)์ ๋ํ๋)
- validate : ์ ํจ์ฑ์ ๊ฒ์ฌ (state code ๊ฐ์ด 20x ์ผ ๋๊ฐ ์ก/์์ ์ด ์ํํ๊ฒ ๋ ๊ฒฝ์ฐ)
- responseJSON : ์๋ต json ๋ฐ์ดํฐ
POST
POST ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๋๋ parameters์ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ์ ๋ฌํฉ๋๋ค.
let url = "https://test.com"
let params = ["id":"testId", "pw":"testPassword"] as Dictionary
AF.request(
url,
method: .post,
parameters: params,
encoding: JSONEncoding(options: []),
headers: ["Content-Type":"application/json", "Accept":"application/json"]
).responseJSON { response in
/** ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ ํ์ฉ */
switch response.result {
case .success(let data):
/** ์ ์์ ์ผ๋ก reponse๋ฅผ ๋ฐ์ ๊ฒฝ์ฐ */
case .failure(let error):
/** ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ */
}
}
File Upload
Alamofire๋ฅผ ์ฌ์ฉํด์ ํ์ผ์ ๋ค์ด๋ก๋ํ๊ฑฐ๋, ์ ๋ก๋ํ๋ ๊ฒ๋ ๊ฐ๋ฅํฉ๋๋ค.
๋จผ์ ์ ๋ฌํ ๋ฐ์ดํฐ๋ฅผ ๊ตฌ์กฐ์ ๋ง๊ฒ ๋ณ๊ฒฝํด์ผ ํฉ๋๋ค.
์ฌ๋ฌ ๊ฐ์ ๋ฐ์ดํฐ๋ for๋ฌธ์ ์ฌ์ฉํด ํ ๋ฒ์ ์ถ๊ฐํฉ๋๋ค.
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
}
์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐํ ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
if let image = imageData?.pngData() {
multipartFormData.append(image, withName: "activityImage", fileName: "\(image).png", mimeType: "image/png")
}
- value : ์ ๋ฌํ ํ์ผ
- withName : key
- fileName : ์๋ฒ์ ์ ๋ก๋ํ ํ์ผ ์ด๋ฆ
- mimeType : ํ์ผ ํ์
let url = "https://test.com"
AF.upload(
multipartFormData: { multipartData in /** ์๋ฒ๋ก ์ ์กํ ๋ฐ์ดํฐ */ },
to: url,
method: .post,
headers: ["Content-Type" : "multipart/form-data"]
).response { response in
if response.error != nil {
/** ํ์ผ ์
๋ก๋ ์คํจ */
} else{
/** ํ์ผ ์
๋ก๋ ์ฑ๊ณต */
}
}
์ด๋, GET์ด๋ POST ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ๋๋ header์ Content-Type๊ฐ application/json์ด์์ต๋๋ค.
ํ์ผ์ ์ ๋ก๋ํ๊ฑฐ๋ ๋ค์ด๋ก๋ํ ๋๋ Content-Type์ multipart/form-data๋ก ์์ฑํด์ผ ํฉ๋๋ค.
File Download
let url = "https://test.com/test.jpg"
/** ํ์ผ์ ์ ์ฅํ๊ธฐ ์ํ ์ฝ๋ */
let fileManager = FileManager.default
let appURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileName : String = URL(string: url)!.lastPathComponent /** url์ ๋ง์ง๋ง ๋ฌธ์์ด๋ก ์ด๋ฆ ์ง์ */
let fileURL = appURL.appendingPathComponent(fileName)
/** ํ์ผ ๊ฒฝ๋ก ์ง์ ๋ฐ ๋ค์ด๋ก๋ ์ต์
์ค์ (์ด์ ํ์ผ ์ญ์ , ๋๋ ํ ๋ฆฌ ์์ฑ) */
let destination: DownloadRequest.Destination = { _, _ in
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download(
url,
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
to: destination
).downloadProgress { progress in
/** ๋ค์ด๋ก๋ progress */
/** progressView๋ฅผ ์ฌ์ฉํ ๋ ์ฌ๊ธฐ์ ์์ฑ */
}.response { response in
if response.error != nil {
/** ํ์ผ ๋ค์ด๋ก๋ ์คํจ*/
} else{
/** ํ์ผ ๋ค์ด๋ก๋ ์ฑ๊ณต*/
}
}
'๐ป Programming > iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS] WKWebView๋ก ์น๋ทฐ(WebView) ๊ตฌํํ๊ธฐ (1) | 2025.02.24 |
---|