Skip to content

Instantly share code, notes, and snippets.

@ykst
Created November 10, 2013 15:47
Show Gist options
  • Save ykst/7399774 to your computer and use it in GitHub Desktop.
Save ykst/7399774 to your computer and use it in GitHub Desktop.
Bundle IDからアプリのアイコンを取得する ref: http://qiita.com/ykst@github/items/ffc09fee78483d04b571
@interface BundleHelper : NSObject
+ (void)retrieveIconURLForBundleID:(NSString *)bundle_id withHandler:(void (^)(NSURL *url))handler;
@end
@implementation BundleHelper
+ (void)retrieveIconURLForBundleID:(NSString *)bundle_id withHandler:(void (^)(NSURL *))handler
{
// カレントロケールからストアの国を推定
static NSString *country;
NSLocale *currentLocale = [NSLocale currentLocale];
country = [currentLocale objectForKey:NSLocaleCountryCode];
// Search APIを叩く => http://itunes.apple.com/<country code>/lookup?bundleID=<bundle id>
NSMutableString *requestUrlString = [[NSMutableString alloc] init];
[requestUrlString appendFormat:@"http://itunes.apple.com/%@/lookup", country];
[requestUrlString appendFormat:@"?bundleId=%@", bundle_id];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:requestUrlString]];
[request setTimeoutInterval:20.0f];
[request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 非同期リクエスト (キューはいいかげん)
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
// APIレスポンスのJSONデコード
NSError *jsonError = nil;
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableLeaves
error:&jsonError];
if (!jsonError) {
NSArray *results = [jsonDictionary objectForKey:@"results"];
if (results.count > 0) {
// 検索結果の一つ目を取得
NSDictionary *dic = results[0];
app.dic = dic;
// 60x60のアイコンのURL
handler([NSURL URLWithString:[dic objectForKey:@"artworkUrl60"]]);
});
} else {
handler(nil);
}
} else {
handler(nil);
}
} else {
handler(nil);
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment