Created
November 10, 2013 15:47
-
-
Save ykst/7399774 to your computer and use it in GitHub Desktop.
Bundle IDからアプリのアイコンを取得する ref: http://qiita.com/ykst@github/items/ffc09fee78483d04b571
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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