728x90
반응형
SMALL
공공데이터 포털의 경우 XML 데이터 포맷이 많아서 기록을 남깁니다. :)
예제 오픈 API: https://www.data.go.kr/tcs/dss/selectApiDataDetailView.do?publicDataPk=15101914#/layer-api-guide
*** +해야 할 것들 ***
Info.plist 에서 App Transport Security Settings -> Allow Arbitrary Loads 를 'YES' 로 해주셔야합니다. :)
API_KEY 숨기는 방법 -> https://trashblog.tistory.com/112
예제코드
//
// TourImageAPI.swift
// TourApp
//
// Created by Byeon jinha on 2023/05/02.
//
import Foundation
class TourImageAPI: NSObject, ObservableObject {
static let shared = TourImageAPI()
@Published var items: [TourImage] = []
private var currentElement: String = ""
private var currentItem: TourImage?
private let auth = Bundle.main.infoDictionary?["API_KEY_E"] as? String
func getMyIP(pageNum: Int) {
// Generate URL and query parameters
let urlString = "https://apis.data.go.kr/B551011/PhotoGalleryService1/galleryList1?numOfRows=5&pageNo=\(pageNum)&MobileOS=iOS&MobileApp=TourApp&serviceKey=\(auth!)"
let urlComponents = URLComponents(string: urlString)
let url = urlComponents?.url
var request = URLRequest(url: url!)
request.addValue("application/xml", forHTTPHeaderField: "Accept")
// request data asynchronously using URLSession
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print("Error: No data")
return
}
// Parse XML data
let parser = XMLParser(data: data)
parser.delegate = self
if parser.parse() {
print("Parsing succeeded")
} else {
print("Parsing failed")
}
}
task.resume()
}
}
extension TourImageAPI: XMLParserDelegate {
func parserDidStartDocument(_ parser: XMLParser) {
print("Parsing started")
}
func parserDidEndDocument(_ parser: XMLParser) {
print("Parsing ended")
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
switch currentElement {
case "galContentId":
currentItem?.galContentId = string
case "galContentTypeId":
currentItem?.galContentTypeId = string
case "galTitle":
currentItem?.galTitle = string
case "galWebImageUrl":
currentItem?.galWebImageUrl = string
case "galCreatedtime":
currentItem?.galCreatedtime = string
case "galModifiedtime":
currentItem?.galModifiedtime = string
case "galPhotographyMonth":
currentItem?.galPhotographyMonth = string
case "galPhotographyLocation":
currentItem?.galPhotographyLocation = string
case "galPhotographer":
currentItem?.galPhotographer = string
case "galSearchKeyword":
currentItem?.galSearchKeyword = string
default:
break
}
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
currentElement = elementName
if elementName == "item" {
currentItem = TourImage()
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "item" {
if let currentItem = currentItem {
DispatchQueue.main.async { [self] in
items.append(currentItem)
}
}
currentItem = nil
}
currentElement = ""
}
}
class TourImage: Identifiable {
var galContentId: String?
var galContentTypeId: String?
var galTitle: String?
var galWebImageUrl: String?
var galCreatedtime: String?
var galModifiedtime: String?
var galPhotographyMonth: String?
var galPhotographyLocation: String?
var galPhotographer: String?
var galSearchKeyword: String?
init() {}
}
728x90
반응형
LIST
'iOS' 카테고리의 다른 글
일급 컬렉션 (0) | 2023.05.05 |
---|---|
One-Time-Pad (0) | 2023.05.04 |
객체지향의 역할, 책임 차이 쉬운 예 (0) | 2023.05.03 |
솔리드(SOLID) 원칙 (0) | 2023.05.03 |
iOS UDP 통신 쉬운 예제 코드 (0) | 2023.04.30 |