直接上代码:
AppVersionManage.h
//
// AppVersionManage.h
// textNav
//
// Created by wxxu on 14/12/18.
// Copyright (c) 2014年 wxxu. All rights reserved.
// app版本管理
#import <Foundation/Foundation.h>
@interface AppVersionManage : NSObject
+ (AppVersionManage *)sharedAppVersionManage;
- (void)appUpdate:(NSString *)appId; //更新
- (void)appScore:(NSString *)appid; //评分
@end
AppVersionManage.m
//
// AppVersionManage.m
// textNav
//
// Created by wxxu on 14/12/18.
// Copyright (c) 2014年 wxxu. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "AppVersionManage.h"
@implementation AppVersionManage
{
NSString *_appId; //app的id
NSString *_trackViewUrl; //app的地址
}
static AppVersionManage *appVersionManage;
+ (AppVersionManage *)sharedAppVersionManage
{
@synchronized(self)
{
if (appVersionManage == nil) {
appVersionManage = [[self alloc] init];
}
return appVersionManage;
}
}
#pragma mark 升级
- (void)appUpdate:(NSString *)appId
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_appId = appId;
//http://itunes.apple.com/lookup?id=xx
//根据appid从苹果服务器上得到json数据,再从json数据中得到版本信息
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// 设置URL
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",_appId]]];
// 设置HTTP方法
[request setHTTPMethod:@"GET"];
// 发送同步請求, 這裡得returnData就是返回得數據楽
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
// NSLog(@"%@",jsonData);
NSArray *infoArray = [jsonData objectForKey:@"results"];
if (infoArray.count != 0) {
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
NSString *latestVersion = [releaseInfo objectForKey:@"version"];
NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];//地址trackViewUrl
_trackViewUrl = trackViewUrl; //地址
double doubleUpdateVersion = [latestVersion doubleValue];
//获取当前version版本信息
//当前运行程序的版本信息,可以在 mainBundle 里面获取:
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
double doubleCurrentVersion = [currentVersion doubleValue];
NSLog(@"doubleCurrentVersion:%f,%f",doubleCurrentVersion,doubleUpdateVersion);
if (doubleCurrentVersion < doubleUpdateVersion) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"app应用名称"
message:@"有新版本,是否升级!"
delegate: self
cancelButtonTitle:@"取消"
otherButtonTitles: @"升级", nil];
alert.tag = 20141;
[alert show];
});
}
}else{
//无此应用
}
});
}
#pragma mark 评分
- (void)appScore:(NSString *)appid
{
_appId = appid;
BOOL neverGrade = [[[NSUserDefaults standardUserDefaults] objectForKey:@"neverGrade"] boolValue];
if(neverGrade != YES) {
//提醒评分
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"app应用名称"
message:@"请去appstore给我们评分"
delegate: self
cancelButtonTitle:@"取消"
otherButtonTitles: @"现在去",@"不再提醒 ", nil];
alert.tag = 20142;
[alert show];
}
}
#pragma mark 升级 评分点击
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == alertView.cancelButtonIndex) return;
switch (alertView.tag) {
case 20142:
{
//评分
// Never Review Button
if (buttonIndex == 2)
{
NSString *number = [NSString stringWithFormat:@"%d", YES];
[[NSUserDefaults standardUserDefaults] setObject:number forKey:@"neverGrade"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
// Review Button
else if (buttonIndex == 1)
{
NSString *number = [NSString stringWithFormat:@"%d", YES];
[[NSUserDefaults standardUserDefaults] setObject:number forKey:@"neverGrade"];
[[NSUserDefaults standardUserDefaults] synchronize];
//"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=
NSString *str = [NSString stringWithFormat:
@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",
_appId ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
}
break;
case 20141:
{
//升级
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:_trackViewUrl]];
}
}
break;
default:
break;
}
}
@end
代码下载地址: 点击打开链接