iPhone应用程序:如何从应用程序ID获取应用程序图标? [英] iPhone App: how to get app icon from app id?

查看:135
本文介绍了iPhone应用程序:如何从应用程序ID获取应用程序图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种从应用ID获取应用图标的方法。你知道怎么做吗?请分享方式。谢谢。



例如
Instagram,我正在寻找的ID是:id389801252



API iTunes Store。它可能会在未来发生变化,但在近期似乎没有变化,所以在这里你是...

  NSString * idString = @id389801252; 

NSString * numericIDStr = [idString substringFromIndex:2]; // @389801252
NSString * urlStr = [NSString stringWithFormat:@http://itunes.apple.com/lookup?id=%@,numericIDStr];
NSURL * url = [NSURL URLWithString:urlStr];
NSData * json = [NSData dataWithContentsOfURL:url];

NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];
NSArray * results = [dict objectForKey:@results];
NSDictionary * result = [results objectAtIndex:0];
NSString * imageUrlStr = [result objectForKey:@artworkUrl100]; //或512,或60

NSURL * artworkURL = [NSURL URLWithString:imageUrlStr];
NSData * imageData = [NSData dataWithContentsOfURL:artworkURL];
UIImage * artworkImage = [UIImage imageWithData:imageData];

请注意,这会使用 NSURL API,因此您最好将其包装在backgorund线程中以获得最佳用户体验。为此程序提供一个ID字符串(上面代码中的 idString ),最后, artworkImage 将包含一个UIImage想要的图像。


I am looking for a way to get the app icon from the app id. Do you know how to do it? Please share the way. Thanks.

e.g Instagram, where the id I'm looking for is: id389801252

https://itunes.apple.com/jp/app/instagram/id389801252?mt=8

I want to get this image:

解决方案

(I composed this answer after 2 minutes of googling... It's just the matter of the correct keyword!)

This is possible using an undocumented documented API of the iTunes Store. It might change in the future, but it doesn't seem to have changed in the near past, so here you are...

NSString *idString = @"id389801252";

NSString *numericIDStr = [idString substringFromIndex:2]; // @"389801252"
NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", numericIDStr];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *json = [NSData dataWithContentsOfURL:url];

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];
NSArray *results = [dict objectForKey:@"results"];
NSDictionary *result = [results objectAtIndex:0];
NSString *imageUrlStr = [result objectForKey:@"artworkUrl100"]; // or 512, or 60

NSURL *artworkURL = [NSURL URLWithString:imageUrlStr];
NSData *imageData = [NSData dataWithContentsOfURL:artworkURL];
UIImage *artworkImage = [UIImage imageWithData:imageData];

Note that this performs two synchronous round-trips using the NSURL API, so you better wrap this in a backgorund thread for maximal user experience. Feed this program an ID string (idString in the code above) and in the end, artworkImage will contain a UIImage with the desired image.

这篇关于iPhone应用程序:如何从应用程序ID获取应用程序图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆