์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- RxSwift
- SnapKit
- visionOS
- SwiftUI
- combine
- Xcode
- CollectionView
- UIKit
- arkit
- BFS
- ํ๋ก๊ทธ๋๋จธ์ค
- XCTest
- Kuring
- BOJ
- Swfit
- ios
- reactorkit
- Lv2
- Flutter
- designpattern
- ๋ฐฑ์ค
- node.js
- MVVM
- swift
- realm
- tableView
- rxcocoa
- TCA
- raywenderlich
- ํจ์คํธ์บ ํผ์ค
- Today
- Total
lgvv98
[REST API] ์นด์นด์ค ์น ๊ฒ์ API - Swift ๋ณธ๋ฌธ
[REST API] ์นด์นด์ค ์น ๊ฒ์ API - Swift
๐ฅ ์บ๋ฟ๋งจ 2021. 9. 25. 20:28โ ์ฐ์ ์ ๋ง ์๋์ ์ด ๊ธ ์ฐ๋ฌ ๋ฐ๋ก!! ๋ฐ๋ก!! ๊ธ ์ฐ๋ฌ ์๋ค!!!
์ฐ์ ์ด๋ฐ๊ฑฐ์ ์ง์ง ๋๋ฌด๋๋ฌด ์ฝํ ๋๋ผ์, ํ๋ํ๋ ์ฐจ๊ทผ์ฐจ๊ทผ ๋ณด๊ณ ์ง๋๊ฐ๋๋ก ํ ๊ฒ.
โ ๋ชฉ์ฐจ
1. ์นด์นด์ค API ๋ฐ๊ธ ๋ฐฉ๋ฒ
2. Xcode ๊ธฐ๋ณธ ์ธํ
3. Codable ๋ง๋ค๊ธฐ
4. Alamofire ์ด์ฉํด์ ํต์ ํ๊ธฐ
5. ์ต์ข ์์ค์ฝ๋
โ ์นด์นด์ค API ๋ฐ๊ธ ๋ฐฉ๋ฒ
์๋์ ์ฌ์ดํธ์ ์ ์ํด์
๐ ์์ iOS๊ฐ ๋ถ์ด ์๋๋ฐ, ๋๋ ๋จผ์ ํด๋ ์ํ์ด๊ณ ์ฒ์ํ๋ ๊ฒฝ์ฐ์๋ ์์ผ๋๊น ๋์ด๊ฐ๊ธฐ!
๐ ํ๋ซํผ ๋ถ๋ถ ํด๋ฆญํด์ ๋ด๊ฐ ์ํ๋ ํ๋ซํผ ๋ฑ๋กํ์๋ฉด ๋ฉ๋๋ค
โก๏ธ ์ฑ ํค ๋ถ๋ถ์ ๋ณด๋ฉด ํค๊ฐ ๋ฐ๊ธ๋์ด ์์ํ ๋ฐ, ์ฐ๋ฆฌ๋ REST API ํค๋ฅผ ์ฌ์ฉํ ์์ ์ด์ผ
โ Xcode ๊ธฐ๋ณธ ์ธํ
๐ Main.Storyboard
๐ ViewController.swift
import UIKit
import Alamofire
class ViewController: UIViewController {
@IBOutlet weak var CallButton: UIButton! // ๋ฒํผ
@IBOutlet weak var APITextView: UITextView! // ํ
์คํธ ๋ทฐ
@IBAction func CallButtonClick(_ sender: Any) {
// ๋ฒํผ ํด๋ฆญ์
}
let url = "https://dapi.kakao.com/v2/search/web"
let headers : HTTPHeaders = [
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"Authorization": "KakaoAK {Rest API KEY}", // ์ด ๋ถ๋ถ์ ๋น์ ์ REST API ํค ๋ฃ์ด์ฃผ๊ธฐ
/* ! ์์ !
"Authorization": "KakaoAK 1abec3hyn81d3nut7as" // KakaoAK ํ๊ณ ๋์ด์ฐ๊ธฐ ์ ์ฌํ ๋ณด๊ธฐ
*/
]
// ๋ฌธ์์ ๋ณด๋ฉด ๋ ์ ํ!
let parameters : [String: Any] = [
"query": "์ผ์ฑ์ ์", // ์๋ ํ์ ๋๋จธ์ง๋ ์ต์
"page": 1,
"size": 15
]
override func viewDidLoad() {
super.viewDidLoad()
}
}
๐ ์ด๊ฒ ๊ฐ์ฅ ๊ธฐ์ด์ ์ธ ์ธํ ์ด์ผ ์ผ๋จ ์ด๋ ๊ฒ ์ค์ ํด ์ค.
๋ฌธ์๋ฅผ ๋ณด๋ฉด์ ํ๋ํ๋ ์ง์ด๋ณด๋ฉด ๋๋ฐฐ๋ก ๋ ์ข๊ฒ ์ง?
https://developers.kakao.com/docs/latest/ko/daum-search/dev-guide
์ด ๋ถ๋ถ์ด ์์ ์ฝ๋๋ก ์ ๋ ๊ฒ ์ ๋ ๊ฒ ์์ฑ๋๋๊ฑฐ์ผ.
์ด๊ฑธ ์ ๋ชปํด? ํ๋ ์ฌ๋๋ ์์ ์ ์์ง๋ง, ์ ๋ง ๋ ์์ด๋ณด๋ผ์... ๋ ํ๋๋ฐ ํ๋ฃจ์ข ์ผ ๊ฑธ๋ ค์ ์ด๋ฐ๊ฑฐ ์๋ ค์ฃผ๋ ํฌ์คํ ์ด ์์ผ๋ฉด ์ธ๋ฐฐ๋ก ์ข์ง ์์๊น? ํด์ ์ ์ด๋ฌ
ํ๋ผ๋ฏธํฐ๋ ๋ฆฌ์คํฐ์ค๋ฅผ ์ง์ ์คํฌ๋กคํ๋ฉด ์ฝ๊ฒ ์ดํดํ ์ ์์๊ฑฐ์ผ!
๐ ์ฐ๋ฆฌ๋ Alamofire๋ฅผ ์ด์ฉํ ์์ ์ธ๋ฐ, ์ด๊ฑด pod ์ค์นํด์ผํ๋ ๋ถ๋ถ์ด์ผ. ๐
๐ ์ํ ๋ฐ์ดํฐ ํ์ธํ๊ธฐ
์ํ ๋ฐ์ดํฐ๋ฅผ ๊ผญ ํ์ธ ํด์ผํด.
๐ ์ฌ๊ธฐ์ ๊ฟํ
JSONํธ๋ฆฌ ๋ถ์ํด์ Codable ์ฝ๊ฒ ๋ง๋ค์ด์ค๋ค.
๋ด๊ฐ Codable ํผ์ ๋ง๋ค๋ค๊ฐ ์๋ชป ๋ง๋ค์ด์, ๊ณ์ ์๋๊ณ ์ง์ง ์ฌํ๋๋ฐ, ์ด๊ฑฐ ์ฌ์ฉํ๋๊น ํ๋ฒ์ ๋์ ๋ ํ๋ณตํ์ด ใ ใ
โ Codable ๋ง๋ค๊ธฐ
โ Alamofire ์ด์ฉํด์ ํต์ ํ๊ธฐ
AF.request(url,
method: .get,
parameters: parameters,
encoding: URLEncoding.default,
headers: headers)
.validate(statusCode: 200..<300)
.responseJSON { response in
//์ฌ๊ธฐ์ ๊ฐ์ ธ์จ ๋ฐ์ดํฐ๋ฅผ ์์ ๋กญ๊ฒ ํ์ฉํ์ธ์.
switch response.result {
case .success(let res):
let resultData = String(data: response.data!, encoding: .utf8)
do {
// ๋ฐํ๊ฐ์ Data ํ์
์ผ๋ก ๋ณํ
print("1")
let jsonData = try JSONSerialization.data(withJSONObject: res, options: .prettyPrinted)
print("2")
let json = try JSONDecoder().decode(Response.self, from: jsonData)
print("3")
self.dataSource = json.documents
print("4")
print(self.dataSource)
print("5")
} catch (let error) {
print("catch error : \(error.localizedDescription)")
}
self.APITextView.text = self.dataSource[2].contents
case .failure(let error):
print("Request failed with error: \(error)")
}
}
โ ์ต์ข ์์ค์ฝ๋
import Foundation
import Foundation
import Alamofire
// MARK: - Welcome
struct Response: Codable {
let meta: Meta
let documents: [Document]
}
// MARK: - Document
struct Document: Codable {
let datetime, contents, title: String
let url: String
}
// MARK: - Meta
struct Meta: Codable {
let totalCount, pageableCount: Int
let isEnd: Bool
enum CodingKeys: String, CodingKey {
case totalCount = "total_count"
case pageableCount = "pageable_count"
case isEnd = "is_end"
}
}
class ViewController: UIViewController {
@IBOutlet weak var CallButton: UIButton!
@IBOutlet weak var APITextView: UITextView!
@IBAction func CallButtonClick(_ sender: Any) {
// ๋ฒํผ ํด๋ฆญ์ ํจ์ ํธ์ถ
getPost2()
}
var dataSource = [Document]()
let url = "https://dapi.kakao.com/v2/search/web"
let headers : HTTPHeaders = [
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"Authorization": "KakaoAK {Your REST API Key}",
]
let parameters : [String: Any] = [
"query": "์ผ์ฑ์ ์",
"page": 1,
"size": 15
]
override func viewDidLoad() {
super.viewDidLoad()
}
func getPost2() {
AF.request(url,
method: .get,
parameters: parameters,
encoding: URLEncoding.default,
headers: headers)
.validate(statusCode: 200..<300)
.responseJSON { response in
//์ฌ๊ธฐ์ ๊ฐ์ ธ์จ ๋ฐ์ดํฐ๋ฅผ ์์ ๋กญ๊ฒ ํ์ฉํ์ธ์.
switch response.result {
case .success(let res):
let resultData = String(data: response.data!, encoding: .utf8)
do {
// ๋ฐํ๊ฐ์ Data ํ์
์ผ๋ก ๋ณํ
let jsonData = try JSONSerialization.data(withJSONObject: res, options: .prettyPrinted)
let json = try JSONDecoder().decode(Response.self, from: jsonData)
self.dataSource = json.documents
} catch (let error) {
print("catch error : \(error.localizedDescription)")
}
self.APITextView.text = self.dataSource[2].contents
case .failure(let error):
print("Request failed with error: \(error)")
}
}
}
}
โ ํฅํ ๋ณด์์ .
1๏ธโฃ HTML ํ๊ทธ ์ง์ฐ๊ธฐ
self.APITextView.text = self.dataSource[2].contents
.replacingOccurrences(of: self.HTMLtag,
with: "",
options: .regularExpression,
range: nil)
์ด๋ ๊ฒ ์์ฑํด์ฃผ๋ฉด ์ ๊ทํํ์์ ํ์ฉํ์ฌ HTML ํ๊ทธ๋ฅผ ์ง์ธ ์ ์๋ค.
2๏ธโฃ input ๋ฐ์์ ๊ฒ์ํ๋ ๋ถ๋ถ ๋ง๋ค๊ธฐ
1. ์คํ ๋ฆฌ๋ณด๋์ SearchBar๋ฅผ ์ถ๊ฐํ ํ์, Delegate๋ฅผ ์ฐ๊ฒฐํด์ค๋ค.
extension ViewController : UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let keyword = searchBar.text
parameters["query"] = keyword
getPost2()
}
}
์ด ์ฝ๋๋ ์์น๋ฐ์์ ๊ฒ์ ๋ฒํผ ํด๋ฆญํ๋ฉด ์คํ๋๋ค.
๐ถ ์์น๋ฐ๋ ๊ตณ์ด IBOutlet์ ์ฐ๊ฒฐ์ ์ํด๋ ๋๋ค.
3๏ธโฃ rx์ด์ฉํด์ ๊ฒ์ ๊ตฌํํ๊ธฐ (๊ณ ๋๋)
'โ ๏ธ deprecated โ ๏ธ > REST API ์ฐ์ต์ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS] TmapAPI Swift ์ฌ์ฉ๋ฒ ์ ๋ฆฌ ๐ (0) | 2022.01.17 |
---|---|
[iOS] KakaoMessage ์ฌ์ฉ๋ฒ(๊ธฐ๋ณธ) โ๏ธ (0) | 2022.01.02 |
๋ค์ด๋ฒ ์ง๋ API โ ๏ธ ๋น๋ ์๋ฌ ํด๊ฒฐ (0) | 2021.09.27 |
[REST API] Alarmofire error ํด๊ฒฐ! (0) | 2021.09.25 |
[REST API] ๐โโ๏ธ ์ฐ์ต์์. (0) | 2021.09.24 |