Swift 날씨 앱 만들기 (1)

API 

 

날씨 OpenAPI 발급 주소

https://openweathermap.org/

 

Сurrent weather and forecast - OpenWeatherMap

Access current weather data for any location on Earth including over 200,000 cities! The data is frequently updated based on the global and local weather models, satellites, radars and a vast network of weather stations. how to obtain APIs (subscriptions w

openweathermap.org

 

API 호출 (도시이름으로 호출)

api.openweathermap.org/data/2.5/weather?q=seoul&appid={API key}

{API Key}에 발급 받은 키 입력

 

 

API Key 숨기기

1. Xcode -> File -> New -> File

 

2. Property List 파일 생성

 

3. String 타입의 값을 하나 만들고, Key에 원하는 값, Value에 API Key 값을 넣음

4. ViewController.swift에 Key에 접근하는 코드 추가

    private var apiKey: String {
        get {
            // 생성한 .plist 파일 경로 불러오기
            guard let filePath = Bundle.main.path(forResource: "APIKey", ofType: "plist") else {
                fatalError("Couldn't find file 'APIKey.plist'.")
            }
            
            // .plist를 딕셔너리로 받아오기
            let plist = NSDictionary(contentsOfFile: filePath)
            
            // 딕셔너리에서 값 찾기
            guard let value = plist?.object(forKey: "OPENWEATHERMAP_KEY") as? String else {
                fatalError("Couldn't find key 'OPENWEATHERMAP_KEY' in 'KeyList.plist'.")
            }
            return value
        }
    }

 

5. git에 Key를 노출하지 않기 위해, gitignore에 생성한 .plist 추가하기

 - 이렇게 함으로써, Key를 노출하지 않고 접근할 수 있게 됨

 

gitignore 생성 방법

https://velog.io/@minji0801/iOS-%EA%B0%9C%EB%B0%9C%EC%9E%90%EB%8A%94-gitignore-%EC%96%B4%EB%96%BB%EA%B2%8C-%EB%A7%8C%EB%93%9C%EB%82%98%EC%9A%94

 

[iOS | Git] gitignore 어떻게 만드나요..?

.gitignore로 민감한 정보는 숨기고 커밋하기

velog.io

 

Swift 날씨 앱 만들기 출처 :

https://velog.io/@dlskawns96/Swift-%EA%B0%84%EB%8B%A8%ED%95%9C-%EB%82%A0%EC%94%A8-%EC%95%B1-%EB%A7%8C%EB%93%A4%EC%96%B4%EB%B3%B4%EA%B8%B0-01-Swift-OpenWeatherMap

 

[Swift] 간단한 날씨 앱 만들어보기 01 (Swift OpenWeatherMap)

[Swift] 간단한 날씨 앱 만들어보기 01 (Swift OpenWeatherMap)

velog.io

'Language > Swift 날씨 앱' 카테고리의 다른 글

Swift 날씨 앱 만들기 (2)  (0) 2022.07.05