[iOS 강좌] 오픈API 이용한 날씨 APP 만들기 - 5. 도시 정보 변경

제공하는 날씨 API에서 도시 정보를 획득하려면, 별도로 citi 정보를 받기 위해 API를 호출해야합니다.

아래의 json파일을 아마존 s3 저장소에 저장하고, 호출을 하였습니다.

https://s3.ap-northeast-2.amazonaws.com/com.ios/city.list.json

Citi정보에 대한 모델 객체를 생성하고, 

#import <Foundation/Foundation.h>
#import "Coord.h"
@interface City : NSObject

@property (nonatomic,strong) id id;
@property (nonatomic,strong) Coord *coord;
@property (nonatomic,strong) NSString *country;
@property (nonatomic,strong) NSString *name;
@end


AFKNetwork를 이용하여 호출을 합니다.


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


호출 결과

[
    {
        "id": 707860,
        "name": "Hurzuf",
        "country": "UA",
        "coord": {
            "lon": 34.283333,
            "lat": 44.549999
        }
    },
    {
        "id": 519188,
        "name": "Novinki",
        "country": "RU",
        "coord": {
            "lon": 37.666668,
            "lat": 55.683334
        }
    },
    {
        "id": 1283378,
        "name": "Gorkhā",
        "country": "NP",
        "coord": {
            "lon": 84.633331,
            "lat": 28
        }
    },
    . ....
]


이중 한국의 도시들만 가져와서, 배열에 담았습니다.
국가코드가 KR인 항목을 오름차순으로 가져왔습니다.
그다음 테이블에 해당 항목을 노출하고, 셀 선택 시 지역코드를 가져와 날씨 를 호출할수 있습니다.

-(void)resCityCode:(id)responseObject{
    // city 배열 생성
    m_cityAllArray =[NSArray arrayWithArray:responseObject];
    NSLog(@"cityArray count = %ld",[m_cityAllArray count]);
    
    // 전체 city 배열 중 국가코드가 KR인 것만 추가
    NSMutableArray *mArray =[[NSMutableArray alloc] init];
    
    for (NSDictionary *dic in m_cityAllArray)
    {
        if([[dic objectForKey:@"country"] isEqualToString:@"KR"])
        {
                [mArray addObject:dic];
        }
    }
    
    // 이름으로 오름 차순 정렬
    NSSortDescriptor *ageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject:ageDescriptor];
    m_cityKRArray = [NSMutableArray  arrayWithArray:[mArray sortedArrayUsingDescriptors:sortDescriptors]];
    
    // 갯수
    NSLog(@"cityKRArray count = %ld",[m_cityKRArray count]);

}

댓글

주간 인기글

카드뉴스 마케팅 팁

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

스타트업 성장 곡선 The Startup Curve

[정보] 인스타그램은 당신의 소리를 '듣고' 있을 수도 있습니다

현수막/명함 등 인쇄물 디자인하기