본문 바로가기
iOS - 실무관련/iOS

Alamofire 관련 코드

by print_soo 2022. 3. 28.

1. Get - queryString

import Foundation
import Alamofire

class FastRequest {
    func getData() {
        let url = "해당URL입력"

        let params = [ "매개변수명" : "값"]

        AF.request(url,
                   method: .get,
                   parameters: params,
                   encoding: URLEncoding.queryString)
            .validate()
            .responseDecodable(of: FastResponse.self) { response in

            switch response.result {
            case .success(let response):
                print("DEBUG>> Success \(response) ")
                //로그인한 사용자 정보 가져오기

            case .failure(let error):
                print("DEBUG>> Error : \(error.localizedDescription)")
            }

        }
    }
}

2. Get - URL만 있을 때

import Foundation
import Alamofire

class FastRequest {
    func getData() {
        let url = "해당URL입력"


        AF.request(url,
                   method: .get,
                   parameters: nil,
                   encoding: URLEncoding.default,
                   headers: nil,
                   interceptor: nil,
                   requestModifier: nil)
            .responseDecodable(of: FastResponse.self) { response in

            switch response.result {
            case .success(let response):
                print("DEBUG>> Success \(response) ")
                //로그인한 사용자 정보 가져오기

            case .failure(let error):
                print("DEBUG>> Error : \(error.localizedDescription)")
            }

        }
    }
}

3. Post

import Foundation
import Alamofire

class Request {
    func postData() {
        let url = "해당 URL입력"
        let params = ["매개변수 명" : 값]

        AF.request(url,
                   method: .post,
                   parameters: params,
                   encoder: JSONParameterEncoder(),
                   headers: ["Content-Type":"application/json; charset=utf-8"])
            .validate()
            .responseDecodable(of: JoinResponse.self) { response in

            switch response.result {
            case .success(let response):
                print("DEBUG>> Success \(response) ")
            case .failure(let error):
                print("DEBUG>> Error : \(error.localizedDescription)")
            }

        }

    }
}

'iOS - 실무관련 > iOS' 카테고리의 다른 글

HexColor 사용하기  (0) 2022.03.28
Navigation Controller 관련 코드  (0) 2022.03.28
모듈화 - iOS  (0) 2022.03.28
화면전환 방법  (0) 2022.03.28
Navigation back button custom하기  (0) 2022.03.28