[iOS 강좌] 오픈API 이용한 날씨 APP 만들기 - 4. open API



날씨 API를 이용하기 위해, 아래의 사이트에서 회원가입 후 key를 발급 받습니다.

https://openweathermap.org/
날씨 오픈 API . 회원가입 후 API  Key 발급 (무료)

여러 API 중 5일 3시간 단위의 API를 사용합니다.
도시명과, APP ID, 발급받은 Key로 API를 호출합니다.

API > 5 day / 3 hour forecast > API DOC
도시명으로 날씨 검색 (3시간 단위, 5일치)
 api.openweathermap.org/data/2.5/forecast?id=524901&APPID=발급받은 API Key

호출방법은 AFKNetwork 오픈소스를 이용 하여, get 방식으로 간단하게 호출할수 있습니다.


NSDictionary * parameters =@{
                         @"id" : @"1835848",
                         @"APPID" : @"f18379cb9ed5dff3d62ad51bab87ba8a",
                         };
[self reqWeatherApi:@"https://api.openweathermap.org/data/2.5/forecast?" parameters:dic];

-(void)reqWeatherApi:(NSString*)url parameters:(NSDictionary*)parameters{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        
    } failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

API 호출 결과
- main 날씨의 메인 정보를 가지고있습니다.
- weather 날씨의 정보를 가지고있습니다.

{

    "cod": "200",
    "message": 0.0057,
    "cnt": 39,
    "list": [
        {
            "dt": 1532314800,
            "main": {
                "temp": 308.77,
                "temp_min": 305.287,
                "temp_max": 308.77,
                "pressure": 1002.18,
                "sea_level": 1023.05,
                "grnd_level": 1002.18,
                "humidity": 67,
                "temp_kf": 3.48
            },
            "weather": [
                {
                    "id": 801,
                    "main": "Clouds",
                    "description": "few clouds",
                    "icon": "02d"
                }
            ],
            "clouds": {
                "all": 20
            },
            "wind": {
                "speed": 1.47,
                "deg": 229.003
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2018-07-23 03:00:00"
        }
}





댓글

주간 인기글

[ubuntu] 신규 계정에 sudo 권한 추가하기

카드뉴스 마케팅 팁

스타트업 성장 곡선 The Startup Curve

SPA(Sigle Page Applications) 란 무엇인가?

[AWS] WinSCP 를 이용해 Linux 인스턴스로 파일 전송하기