336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


앱스토어에서 어플 정보 받아오기


설명

이번 포스팅은 앱스토어에 올라가있는 어플에 대해서 정보를 가져오는 방법입니다. 생각보다 많이 사용이 될까 생각이 들지만 단순 버전관리라던지 여러가지로 사용 할수 있습니다. (저는 버전관리때문에 사용해 봤습니다.)
http://itunes.apple.com/lookup?bundleId=번들 아이디
이 조합으로 쉽게 텍스트 파일을 받아온 후 JSON 형식으로 받아서 사용하면 됩니다.


사용환경

* Swift 4.2
* XCode 10.0

코드

//Objective-C
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString* appID = infoDictionary[@"CFBundleIdentifier"];
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]];
NSData* data = [NSData dataWithContentsOfURL:url];
NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

if ([lookup[@"resultCount"] integerValue] == 1){
    NSString* appStoreVersion = lookup[@"results"][0][@"version"];
     NSString* currentVersion = infoDictionary[@"CFBundleShortVersionString"];

    NSLog(@"Need to update [%@ != %@]", appStoreVersion, currentVersion);
}

//Swift
guard let infoDic = Bundle.main.infoDictionary,
            let appID = infoDic["CFBundleIdentifier"] as? String,
            let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(appID)") else {
            print("gaurd error")
            return
        }

        do {
            let data = try Data(contentsOf: url)
            let json = try JSONSerialization.jsonObject(with: data, options: [])
            guard let dic = json as? NSDictionary else {
                print("guard error")
                return
            }
            if dic.object(forKey: "resultCount") as? Int != 0 {
                //Do Something
                print(dic)
            } else {
                print("not result")
            }
        } catch {
            print(error.localizedDescription)
        }

맞치며

해당 아이디의 앱스토어에 올라가있는 정보입니다. 생각보다 많은 정보를 내려줍니다. 앱스토어 홈페이지가 아니 다른 사이트에서 보여줄때 이런식으로 정보를 받아와서 보여주는거 같습니다. 여담이지만 JSON은 아직까지 라이브러리 통해서 쓰는게 나은거 같습니다.. 틀린점이나 궁금한점 있으면 댓글 남겨주세요. 감사합니다 :)



+ Recent posts