๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ฑ Mobile/iOS

[iOS - Network] iOS์—์„œ ์„œ๋ฒ„ ํ†ต์‹  ํ•˜๋Š” ๋ฐฉ๋ฒ• (URL Session, Alamofire, Moya)

์„œ๋ฒ„ ํ†ต์‹ ์ด๋ž€?

๐Ÿ’ก ํด๋ผ์ด์–ธํŠธ(ํ”„๋ก ํŠธ์—”๋“œ)์™€ ์„œ๋ฒ„(๋ฐฑ์—”๋“œ)๊ฐ€ ์„œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๊ฒƒ

 

 

๊ฐœ์ธ ํ”„๋กœ์ ํŠธ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋งŽ์•„๋ดค์ž 10๊ฐœ ์ •๋„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ๋•Œ๋ฌธ์— Dummy๋ฅผ ํ™œ์šฉํ•ด์„œ ๋„ฃ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋‚ ์”จ ์•ฑ์„ ์˜ˆ์‹œ๋กœ ๋“ค๋ฉด ์ „๊ตญ์˜ ์ง€์—ญ๋ณ„ ๋‚ ์”จ, ์‹œ๊ฐ„๋Œ€๋ณ„, ๊ฐ•์ˆ˜ํ™•๋ฅ , ์ตœ๊ณ  ์˜จ๋„, ์ตœ์ € ์˜จ๋„ ๋“ฑ๋“ฑ…. ๋„ˆ๋ฌด ๋งŽ์€ ๋ฐ์ดํ„ฐ๋“ค์„ dummy๋กœ ๋‹ค๋ฃจ๋Š” ๊ฑด ๋ถˆ๊ฐ€๋Šฅํ•œ ์ผ์ด๋‹ค.

 

๋•Œ๋ฌธ์—, ํด๋ผ๋ฆฌ์–ธํŠธ์—์„œ ํ•„์š”ํ•œ ์ •๋ณด๊ฐ€ ์žˆ์„ ๋•Œ๋งˆ๋‹ค ์„œ๋ฒ„์—๊ฒŒ ์š”์ฒญ์„ ํ•˜๊ณ , ์„œ๋ฒ„๋Š” ๊ทธ ์š”์ฒญ์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค. ์ด๋ฅผ ์„œ๋ฒ„ ํ†ต์‹ ์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

iOS์—์„œ๋Š” ๋Œ€ํ‘œ์ ์œผ๋กœ URLSession, Alamofire, Moya๋กœ ์„œ๋ฒ„ ํ†ต์‹ ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

URL Session

๐Ÿ’ก - Swift์—์„œ๋Š” URLSession์„ ์ด์šฉํ•˜์—ฌ API๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

  • URLSession์€ ์›น ์„œ๋ฒ„์™€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ์ฒด๋‹ค.
  • URLSession์€ Apple์ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ๋„คํŠธ์›Œํฌ API๋กœ, iOS, macOS, tvOS, watchOS ๋“ฑ ๋‹ค์–‘ํ•œ ํ”Œ๋žซํผ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

URL ๊ฐ์ฒด ์ƒ์„ฑ

let session = URLSession.shared
let url = URL(string: "<https://api.example.com/data>")

 

URLRequest ๊ฐ์ฒด ์ƒ์„ฑ

let request = URLRequest(url: url!)

URLSessionDataTask ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์‹คํ–‰

let task = session.dataTask(with: request) { (data, response, error) in
    if let error = error {
        print("Error: \\\\(error.localizedDescription)")
        return
    }

    guard let data = data else {
        print("No data received")
        return
    }

    do {
        let json = try JSONSerialization.jsonObject(with: data, options: [])
        print(json)
    } catch let error {
        print("Error: \\\\(error.localizedDescription)")
    }
}
task.resume()

 

Alamofire

๐Ÿ’ก Alamofire ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ API๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

  • Alamofire๋Š” Swift์—์„œ HTTP ๋„คํŠธ์›Œํ‚น์„ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.
  • Alamofire๋Š” Swift ๊ธฐ๋ฐ˜์˜ HTTP ๋„คํŠธ์›Œํ‚น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, URLSession์„ ๋” ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ถ”์ƒํ™”ํ•œ ๊ฒƒ์ด๋‹ค.

Alamofire ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

pod 'Alamofire'

 

Alamofire์„ ์ด์šฉํ•œ API ํ˜ธ์ถœ

import Alamofire

let url = "<https://api.example.com/data>"

AF.request(url, method: .get).responseJSON { response in
    switch response.result {
    case .success(let value):
        print(value)
    case .failure(let error):
        print(error.localizedDescription)
    }
}

 

Moya

๐Ÿ’ก  Moya๋Š” Alamofire๋ฅผ ๋” ์ถ”์ƒํ™”ํ•œ ๋„คํŠธ์›Œํฌ ์ถ”์ƒํ™” ๊ณ„์ธต์ด๋‹ค.

  • Moya๋Š” ๋„คํŠธ์›Œํฌ ๊ณ„์ธต์„ ํ…œํ”Œ๋ฆฟํ™”ํ•˜์—ฌ ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ๊ฐ€๋…์„ฑ์„ ๋†’์ธ๋‹ค.

 

๊ทธ๋ž˜์„œ 3์ค„์š”์•ฝ ๋ญ”๋ฐ์š”?

URLSession์€

๐Ÿ‘๐Ÿป๋กœ์šฐ๋ ˆ๋ฒจ์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ,

๐Ÿ‘Ž๐Ÿป์‚ฌ์šฉ์ด ๋ณต์žกํ•˜๊ณ  ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์ด ์ข‹์ง€ ์•Š๋‹ค.

 

Alamofire๋Š”

๐Ÿ‘๐ŸปFoundation Networking์„ ๊ธฐ๋ฐ˜์œผ๋กœํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•ด ๋„คํŠธ์›Œํ‚น ์ž‘์—…์„ ๋‹จ์ˆœํ™” ํ•ด์ฃผ์ง€๋งŒ

๐Ÿ‘Ž๐ŸปiOS ์•ฑ์„ ๊ฐœ๋ฐœํ•  ๋•Œ ๊ฐ€์žฅ ๋ณดํŽธ์ ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ด์ง€๋งŒ, ์œ ์ง€๋ณด์ˆ˜์™€ ์œ ๋‹› ํ…Œ์ŠคํŠธ๊ฐ€ ํž˜๋“ค๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

 

Moya๋Š”

URLSession์„ ์ถ”์ƒํ™”ํ•œ Alamofire๋ฅผ ๋‹ค์‹œ ์ถ”์ƒํ™”ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ

๐Ÿ‘๐ŸปNetwork Layer๋ฅผ ํ…œํ”Œ๋ฆฟํ™” ํ•ด์„œ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์—ฌ์ฃผ๋ฉด์„œ ๊ฐœ๋ฐœ์ž๊ฐ€ request, response์—๋งŒ ์‹ ๊ฒฝ์„ ์“ฐ๋„๋ก ํ•ด์ค€๋‹ค.

 

 

 

728x90