NSString *currentVersion = [self getLocalVersion];
NSString *lastVersion = [self getCurrentAppStoreVersion];
if (![lastVersion isEqualToString:currentVersion]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新埠对,是否前往更新返劲?" delegate:self cancelButtonTitle:@"關(guān)閉" otherButtonTitles:@"更新", nil];
alert.tag = 10000;
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本為最新版本" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
alert.tag = 10001;
[alert show];
}
//本機(jī)版本號(hào)
- (NSString *)getLocalVersion{
NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [appInfo objectForKey:@"CFBundleVersion"];
return currentVersion;
}
//線上版本號(hào)
- (NSString *)getCurrentAppStoreVersion
{
NSString *URL = @"http://itunes.apple.com/lookup?id=550383618";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = nil;
NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableContainers error:nil];
NSArray *infoArray = [dic objectForKey:@"results"];
NSString *lastVersion = nil;
if ([infoArray count]) {
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
lastVersion = [releaseInfo objectForKey:@"version"];
NSLog(@"HAHAHAHA%@",lastVersion);
}
return lastVersion;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag==10000) {
if (buttonIndex==1) {
NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/us/app/id550383618?mt=8"];
[[UIApplication sharedApplication]openURL:url];
}
}
}
當(dāng)前版本
- (NSString *)getCurrentLocalVersion{
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
CFShow(infoDic);
NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];
}
線上版本
- (NSString *)getCurrentAppStoreVersion
{
NSString *URL = @"http://itunes.apple.com/lookup?id=550383618";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = nil;
NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableContainers error:nil];
NSArray *infoArray = [dic objectForKey:@"results"];
NSString *lastVersion = nil;
if ([infoArray count]) {
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
lastVersion = [releaseInfo objectForKey:@"version"];
NSLog(@"HAHAHAHA%@",lastVersion);
}
return lastVersion;
}
下面是解析
- (NSString *)getCurrentAppStoreVersion
{
// 具體步驟如下:
// 具體步驟如下:
// 1皂冰,用 POST 方式發(fā)送請(qǐng)求:
// http://itunes.apple.com/search?term=你的應(yīng)用程序名稱&entity=software
//
// 更加精準(zhǔn)的做法是根據(jù) app 的 id 來查找:
// http://itunes.apple.com/lookup?id=你的應(yīng)用程序的ID
//#define APP_URL http://itunes.apple.com/lookup?id=你的應(yīng)用程序的ID
//
// 你的應(yīng)用程序的ID 是 itunes connect里的 Apple ID
//
// 2拣技,從獲得的 response 數(shù)據(jù)中解析需要的數(shù)據(jù)。因?yàn)閺?appstore 查詢得到的信息是 JSON 格式的,所以需要經(jīng)過解析脏里。解析之后得到的原始數(shù)據(jù)就是如下這個(gè)樣子的:
// {
// resultCount = 1;
// results = (
// {
// artistId = 開發(fā)者 ID;
// artistName = 開發(fā)者名稱;
// price = 0;
// isGameCenterEnabled = 0;
// kind = software;
// languageCodesISO2A = (
// EN
// );
// trackCensoredName = 審查名稱;
// trackContentRating = 評(píng)級(jí);
// trackId = 應(yīng)用程序 ID;
// trackName = 應(yīng)用程序名稱";
// trackViewUrl = 應(yīng)用程序介紹網(wǎng)址;
// userRatingCount = 用戶評(píng)級(jí);
// userRatingCountForCurrentVersion = 1;
// version = 版本號(hào);
// wrapperType = software;
// }
// );
// }
//
// 然后從中取得 results 數(shù)組即可,具體代碼如下所示:
//
// NSDictionary *jsonData = [dataPayload JSONValue];
// NSArray *infoArray = [jsonData objectForKey:@"results"];
// NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
// NSString *latestVersion = [releaseInfo objectForKey:@"version"];
// NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];
//
// 如果你拷貝 trackViewUrl 的實(shí)際地址虹曙,然后在瀏覽器中打開迫横,就會(huì)打開你的應(yīng)用程序在 appstore 中的介紹頁面。當(dāng)然我們也可以在代碼中調(diào)用 safari 來打開它酝碳。
// UIApplication *application = [UIApplication sharedApplication];
// [application openURL:[NSURL URLWithString:trackViewUrl]];
//
// 具體代碼如下:
//{
NSString *URL = @"http://itunes.apple.com/lookup?id=550383618";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = nil;
NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
// NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
// NSDictionary *dic = [results JSONValue];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableContainers error:nil];
NSArray *infoArray = [dic objectForKey:@"results"];
if ([infoArray count]) {
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
NSString *lastVersion = [releaseInfo objectForKey:@"version"];
NSLog(@"HAHAHAHA%@",lastVersion);
}
//}
NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com"];
[[UIApplication sharedApplication]openURL:url];
return @"1234";
}