Request GET > IOS 프로그래밍

본문 바로가기

IOS 프로그래밍

IOS 프로그래밍

2. Request GET

페이지 정보

작성자 관리자 댓글 0건 조회 5,301회 작성일 20-02-11 16:48

본문

2. Request GET

guard let url = URL(string: "https://www.leelab.co.kr/item/getItems.php") else {return } 


var request = URLRequest(url: url)


request.httpMethod = "GET"


request.addValue("application/json", forHTTPHeaderField: "Content-Type")

request.addValue(token, forHTTPHeaderField: "Authorization")

let session = URLSession.shared

session.dataTask(with: request) { (data, response, error) in

       guard error == nil else {

                    print(" >> error (1)")

                    return

       }

       guard let data = data else {

                    print(" >> error (2)")

                    return

       }

                

       do {

            if let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String : Any] {

                        //print(json)


                        let code = json["code"] as! Int

                        if code == 200 {

                            DispatchQueue.main.async {

                                 self.createTransaction() 

                            }

                        }else {

                            DispatchQueue.main.async {

                                print( "Item is not found.")

                            }

                        }

                       

             }

                    

       }catch let error {

                    print(error.localizedDescription)

                    

       }

                

}.resume()




Copyright © LEELAB.CO.KR. All rights reserved.