步驟1
id 指的是在你的應用在蘋果商店中的id號 查看方法也簡單:用電腦itunes打開,搜索自己的應用,然后復制網址,網址上有就是這個id號
NSURLRequest *rep = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://itunes.apple.com/lookup?id=1160672874"]];
[NSURLConnection connectionWithRequest:rep delegate:self];
步驟2
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSError *error;
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSDictionary *appInfo = (NSDictionary*)jsonObject;
NSArray *infoContent = [appInfo objectForKey:@"results"];
NSString *version = [[infoContent objectAtIndex:0]objectForKey:@"version"];
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleShortVersionString"];
NSLog(@"商店的版本是%@ 當前的版本是%@",version,currentVersion);
if ([currentVersion doubleValue]<[version doubleValue]) {
NSLog(@"new:%@ old:%@",currentVersion,version);
UIAlertView *alerV = [[UIAlertView alloc]initWithTitle:@"發現新版本" message:@"App又添新功能啦,大量驚喜等您來發現" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:@"前往更新", nil];
[alerV show];
}
}