1、效果圖
提示新版本
提示新版本(讀取了AppStore寫的更新方案)
2、關鍵點
請求http://itunes.apple.com/lookup?id=968615456
,就會返回關于這個應用的詳細內容的JSON數據,后面就簡單了吧
我的應用返回的JSON數據(已經格式化):
{
"resultCount": 1,
"results": [
{
"advisories": [
"Unrestricted Web Access"
],
"artistId": 914453386,
"artistName": "he tiancong",
"artistViewUrl": "https://itunes.apple.com/us/artist/he-tiancong/id914453386?uo=4",
"artworkUrl100": "http://is5.mzstatic.com/image/pf/us/r30/Purple5/v4/6f/89/92/6f8992e4-5c10-be87-9242-d933ddb0780a/mzl.jxbacfmg.png",
"artworkUrl512": "http://is5.mzstatic.com/image/pf/us/r30/Purple5/v4/6f/89/92/6f8992e4-5c10-be87-9242-d933ddb0780a/mzl.jxbacfmg.png",
"artworkUrl60": "http://is3.mzstatic.com/image/pf/us/r30/Purple3/v4/2e/cc/a9/2ecca990-b82a-a238-793a-a16b59b56389/AppIcon60x60_U00402x.png",
"bundleId": "CampusOfGLUT",
"contentAdvisoryRating": "17+",
"currency": "USD",
"description": "桂林理工大學——校園通正式上線!
應用提供:
1、學校新聞、通知等動態;
2、學校學院、學習等、
3、學習 、生活等多欄目,分享和交流平臺~
歡迎大家提供建議和評價!
郵件:ihetiancong@qq.com
QQ:3027201361",
"features": [
"iosUniversal"
],
"fileSizeBytes": "6436734",
"formattedPrice": "Free",
"genreIds": [
"6017",
"6012"
],
"genres": [
"Education",
"Lifestyle"
],
"ipadScreenshotUrls": [
"http://a1.mzstatic.com/us/r30/Purple3/v4/cf/36/c1/cf36c120-3798-303d-6ae6-cb70da8c7b18/screen480x480.jpeg",
"http://a3.mzstatic.com/us/r30/Purple3/v4/4c/34/19/4c3419c4-c777-f202-0f5a-a1e733bf37a8/screen480x480.jpeg",
"http://a2.mzstatic.com/us/r30/Purple5/v4/9e/9b/35/9e9b3592-4e93-148b-0fd8-73d8ed389388/screen480x480.jpeg"
],
"isGameCenterEnabled": false,
"kind": "software",
"languageCodesISO2A": [
"EN",
"FR",
"DE",
"JA",
"KO",
"PL",
"RU",
"ZH",
"ES",
"ZH",
"UK"
],
"minimumOsVersion": "8.1",
"price": 0.0,
"primaryGenreId": 6017,
"primaryGenreName": "Education",
"releaseDate": "2015-03-18T09:34:28Z",
"releaseNotes": "——————那些年,最后一次更新的版本——————
1、優化iPhone和iPad下顯示效果
2、增加話題圖標,內容更加醒目
3、增加一些可有可無的更新
——————那些年,我們一起說過的話題——————
1、非常感謝那些年i被拒絕的女孩,她們讓我充實成長
2、非常感謝那些年i被關照的老師,他們讓我無憂學習
3、非常感謝那些年i被信任的同學,他們讓我珍惜想念
4、非常感謝那些年i被堅持的自己,你們讓我相信自己
——————最后說明——————
1、應用為i畢業設計作品,不提供商業信息,感謝大家支持
2、應用中的學校新聞只提參考,由于解析原因,可能個別新聞無法顯示,真實動態請瀏覽(www.glut.edu.cn)
3、校園話題的意義是希望能將學校的信息通過移動互聯網重新整合在一起,這是一個信息未來的趨勢
4、其它問題請聯系:ihetiancong@qq.com
——————謝謝大家——————",
"screenshotUrls": [
"http://a2.mzstatic.com/us/r30/Purple1/v4/1f/bf/32/1fbf321a-c22b-f938-0d84-17e65730e7a9/screen1136x1136.jpeg",
"http://a5.mzstatic.com/us/r30/Purple3/v4/c9/92/ec/c992ec71-7576-6334-3ebb-4aa857880219/screen1136x1136.jpeg",
"http://a1.mzstatic.com/us/r30/Purple5/v4/72/27/ae/7227aed1-62fa-6946-e63e-52090ce6a021/screen1136x1136.jpeg",
"http://a4.mzstatic.com/us/r30/Purple2/v4/13/46/d3/1346d396-4126-ab48-ae15-b0038b92bd68/screen1136x1136.jpeg"
],
"sellerName": "he tiancong",
"sellerUrl": "http://weibo.com/iHTCapp",
"supportedDevices": [
"iPhone5c",
"iPhone6Plus",
"iPodTouchFifthGen",
"iPadThirdGen4G",
"iPad2Wifi",
"iPadFourthGen",
"iPhone5s",
"iPhone4S",
"iPadThirdGen",
"iPad23G",
"iPhone6",
"iPadFourthGen4G",
"iPhone5",
"iPadMini",
"iPadMini4G"
],
"trackCensoredName": "桂林理工大學---校園通",
"trackContentRating": "17+",
"trackId": 968615456,
"trackName": "桂林理工大學---校園通",
"trackViewUrl": "https://itunes.apple.com/us/app/gui-lin-li-gong-da-xue-xiao/id968615456?mt=8&uo=4",
"version": "1.3",
"wrapperType": "software"
}
]
}
3、檢查更新Demo
- 檢查更新
[AFNetworkingUtils getRequestWithUrl:@"http://itunes.apple.com/lookup?id=968615456" afterExecute:^(NSDictionary *resultDic) {
float version =[[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] floatValue];
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
float currentVersion = [[infoDic valueForKey:@"CFBundleShortVersionString"] floatValue];
if(version>currentVersion){
NSString *alertTitle=[@"桂林理工大學-校園通v" stringByAppendingString:[NSString stringWithFormat:@"%0.1f",version]];
NSString *alertMsg=@"是否要更新?";
//NSString *alertMsg = [[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"releaseNotes"]
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:alertTitle message:alertMsg delegate:self cancelButtonTitle:@"稍后更新" otherButtonTitles:@"立即更新", nil];
[alertView show];
}
} onError:^{
}];
- 跳到AppStore更新
NSString *url = @"https://itunes.apple.com/us/app/gui-lin-li-gong-da-xue-xiao/id968615456?mt=8&uo=4";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
4、自定義成一個工具
- 使用時調用這個方法就行
#pragma mark - 檢查版本更新
-(void) checkUpdate{
InformationHandleTool * tool = [InformationHandleTool sharedInfoTool];
[tool checkUpdateWithAppID:@"968615456" success:^(NSDictionary *resultDic, BOOL isNewVersion, NSString *newVersion) {
if (isNewVersion) {
[self showUpdateView:newVersion];
}
} failure:^(NSError *error) {
;
}];
}
- 網絡請求方法-(void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNewVersion , NSString * newVersion))success failure:(void (^)(NSError *error))failure
#pragma mark - 檢查版本更新
\- (void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNewVersion , NSString * newVersion))success failure:(void (^)(NSError *error))failure
{
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
manager.requestSerializer=[AFHTTPRequestSerializer serializer];
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
NSString *encodingUrl=[[@"http://itunes.apple.com/lookup?id=" stringByAppendingString:appID] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[manager GET:encodingUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
NSString * versionStr =[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
float version =[versionStr floatValue];
//self.iTunesLink=[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"trackViewUrl"];
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
float currentVersion = [[infoDic valueForKey:@"CFBundleShortVersionString"] floatValue];
if(version>currentVersion){
success(resultDic, YES, versionStr);
}else{
success(resultDic,NO ,versionStr);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
}
- 提醒方法- (void)showUpdateView:(NSString *)newVersion
** 注:這個是iOS8以后的UIAlertController **
\- (void)showUpdateView:(NSString *)newVersion
{
NSString *alertMsg=[[@"桂林理工大學-校園通v" stringByAppendingString:[NSString stringWithFormat:@"%0.1f",[newVersion floatValue]]] stringByAppendingString:@",趕快體驗最新版本吧!"];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"發現新版本" message:alertMsg preferredStyle:UIAlertControllerStyleAlert];
// 添加按鈕
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *str = @"https://itunes.apple.com/us/app/gui-lin-li-gong-da-xue-xiao/id968615456?mt=8&uo=4";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
5、檢查增強
- 服務器版本跟本地版本可以隨意,比如服務器1.4.1 ,本地1.4,也能檢查了。
NSString * serverVersion =[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
NSString * localVersion = [SystemUtils getVersionSting];
//以"."分隔數字然后分配到不同數組
NSArray * serverArray = [serverVersion componentsSeparatedByString:@"."];
NSArray * localArray = [localVersion componentsSeparatedByString:@"."];
for (int i = 0; i < serverArray.count; i++) {
//以服務器版本為基準,判斷本地版本位數小于服務器版本時,直接返回(并且判斷為新版本,比如服務器1.5.1 本地為1.5)
if(i > (localArray.count -1)){
//有新版本,提示!
break;
}
//有新版本,服務器版本對應數字大于本地
if ( [serverArray[i] intValue] > [localArray[i] intValue]) {
//有新版本,提示!
break;
}
}
- 如果有什么疑問,可以在評論區一起討論;
- 如果有什么不正確的地方,歡迎指導!
注:本文首發于 iHTCboy's blog,如若轉載,請注明來源。