라이브러리를 추가하는 방법
- CocoaPods: 점유율이 제일 높다
- Carthage: 코코아팟 다음으로 많이 쓰는 것
- Swift Package Manager: 애플이 제일 권장하는 방법, 깔끔하다
→ 여기서는 'Swift Package Manager'를 사용하였다.
Xcode → File → Swift Packages → Add package dependency... → URL 추가하기
alamofire
네트워크 통신을 도와주는 라이브러리
https://github.com/Alamofire/Alamofire
Alamofire/Alamofire
Elegant HTTP Networking in Swift. Contribute to Alamofire/Alamofire development by creating an account on GitHub.
github.com
https://github.com/Alamofire/Alamofire.git
import Alamofire
Swifty JSON
서버의 결과로 받은 JSON 데이터를 Swift 에서 깔끔하게 사용할 수 있는 라이브러리
https://github.com/SwiftyJSON/SwiftyJSON
SwiftyJSON/SwiftyJSON
The better way to deal with JSON data in Swift. Contribute to SwiftyJSON/SwiftyJSON development by creating an account on GitHub.
github.com
https://github.com/SwiftyJSON/SwiftyJSON.git
import SwiftyJSON
http 통신을 앱에서 호출 허용
iOS 9부터 http 통신을 앱에서 호출하는 것을 기본으로 금지하고 있다 → 보안상 금지
요즘에는 안전한 https 통신을 권장
여기서는 http 통신을 하기위해 허용으로 바꿔줌
Info.plist
GET 요청
ReviewTableViewController.swift
화면이 나타나기 직전에 데이터 가져오기
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
AF.request("http://spartacodingclub.shop/review").responseJSON { (response) in
if let value = response.value {
let json = JSON(value)
self.reviews = json["reviews"].arrayValue
self.tableView.reloadData()
}
}
}
테이블뷰
// sectopm 갯수
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
// row 갯수
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.reviews.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReviewCell", for: indexPath)
// Configure the cell...
let review = reviews[indexPath.row]
cell.textLabel?.text = "\(review["author"].stringValue) - \(review["title"].stringValue)"
cell.detailTextLabel?.text = review["review"].stringValue
return cell
}
POST 요청
WriteViewController.swift
@IBAction func clickedDoneButton(_ sender: Any) {
let title = titleTextField.text ?? ""
let author = authorTextField.text ?? ""
let review = reviewTextField.text ?? ""
if title.count == 0 || author.count == 0 || review.count == 0 {
let alert = UIAlertController(title: "책리뷰", message: "모두 입력해주세요!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "확인", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
let parameters = [
"title_give": title,
"author_give": author,
"review_give": review
]
AF.request("http://spartacodingclub.shop/review", method: .post, parameters: parameters).responseJSON { (response) in
if let value = response.value {
let json = JSON(value)
print(json)
self.navigationController?.popViewController(animated: true)
}
}
}
NSUnknownKeyException
갑자기 발생한 오류
이유는 IBOutlet 으로 연결했는데 IBOutlet 이름을 바꿔서 Outlet 연결이 남아있었다.
연결 해제하고 다시 연결하면 해결~
'iOS > COURSE' 카테고리의 다른 글
스파르타 코딩클럽 iOS 앱개발 기초반 4기 회고 (2) | 2021.07.29 |
---|---|
[스파르타코딩클럽] iOS 앱개발 기초반 5주차 (0) | 2021.07.29 |
[스파르타코딩클럽] iOS 앱개발 기초반 3주차 (0) | 2021.07.18 |
[스파르타코딩클럽] iOS 앱개발 기초반 2주차 (2) | 2021.07.18 |
[스파르타코딩클럽] iOS 앱개발 기초반 1주차 (2) | 2021.06.12 |