版本更新检测
现在大部分应用都有版本更新检测,当有新版本时弹出提示用户更新,有时还需要强制更新(很少用,影响体验)。
更新之后,最好清理本地的缓存数据,因为新版本有可能对某些数据对象结构有更改,上一版本缓存的数据结构不能满足。
构造一个版本信息的类
typedef NS_ENUM(NSInteger, PGVersionUpdateType) {
VERION_UPDATE_TYPE_NONE = 1,//没有更新
VERION_UPDATE_TYPE_OPTIONAL,//可选更新
VERION_UPDATE_TYPE_FORCE,//强制更新
};
@interface PGVersionObject : PGBaseObj
/*
版本号
*/
@property(nonatomic, strong)NSString *szVersion;
/*
更新地址
*/
@property(nonatomic, strong)NSString *szUrl;
/*
更新内容简述
*/
@property(nonatomic, strong)NSString *szDesc;
/*
0:没有更新,1:有更新,2:强制更新
*/
@property(nonatomic, assign)PGVersionUpdateType updateType;
@end
版本检测,弹框提示我都放到个类里面处理了:
@interface PGVersionManager : NSObject
/*
检测版本更新
*/
+ (void)checkVersion;
@end
#define VERSION_RETRY_COUNT 5
@interface PGVersionManager ()<PGApiDelegate>
/*
上次检测更新的时间
*/
@property(nonatomic, strong)NSDate *lastCheckDate;
/*
是否有强制更新
*/
@property(nonatomic, assign)BOOL needUpdate;
/*
检测版本失败重试次数
*/
@property(nonatomic, assign)NSInteger nRetryCount;
@end
@implementation PGVersionManager
+ (PGVersionManager *)shareInstance
{
static PGVersionManager *s_versionManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
s_versionManager = [[PGVersionManager alloc] init];
});
return s_versionManager;
}
- (id)init
{
if(self = [super init])
{
self.needUpdate = NO;
self.lastCheckDate = nil;
self.nRetryCount = VERSION_RETRY_COUNT;
}
return self;
}
+ (void)checkVersion
{
[PGVersionManager shareInstance].nRetryCount = VERSION_RETRY_COUNT;
if([PGVersionManager shareInstance].needUpdate ||
[PGVersionManager shareInstance].lastCheckDate == nil ||
[NSDate numDayFromDate:[PGVersionManager shareInstance].lastCheckDate toDate:[NSDate date]] >= 1)
{
[[PGVersionManager shareInstance] getVersionInfo];
}
}
- (void)getVersionInfo
{
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
[PGRequestManager startPostClient:API_TYPE_VERSION_CHECK param:@{@"app_version":version} target:self extendParam:nil];
}
#pragma mark - PGApiDelegate
- (void)dataRequestFinish:(PGResultObject *)resultObj apiType:(PGApiType)apiType
{
if(resultObj.nCode == 0)
{
self.lastCheckDate = [NSDate date];
PGVersionObject *versionObj = (PGVersionObject *)resultObj.dataObject;
if(versionObj.updateType == VERION_UPDATE_TYPE_OPTIONAL)
{
[[PGApp appRootController] showAskAlertTitle:[NSString stringWithFormat:@"发现新版本:%@",versionObj.szVersion] message:versionObj.szDesc tag:1000 action:^(NSInteger alertTag, NSInteger actionIndex) {
if(actionIndex == 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:versionObj.szUrl]];
});
}
} cancelActionTitle:@"更新" otherActionsTitles:@"取消",nil];
}
else if(versionObj.updateType == VERION_UPDATE_TYPE_FORCE)
{
[[PGApp appRootController] showAskAlertTitle:[NSString stringWithFormat:@"发现新版本:%@",versionObj.szVersion] message:versionObj.szDesc tag:1000 action:^(NSInteger alertTag, NSInteger actionIndex) {
if(actionIndex == 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:versionObj.szUrl]];
});
}
} cancelActionTitle:@"更新" otherActionsTitles:nil];
}
}
else
{
//失败重试
if(self.nRetryCount > 0)
{
self.nRetryCount -= 1;
[self getVersionInfo];
}
}
}
@end
更新完成后最好清理本地缓存,这时只需在PGCacheManager里面添加一函数方法就可以了。
+ (void)clearCacheDataForNewVersion
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int bClear = [[defaults stringForKey:@"bCacheClear"] intValue];
NSInteger oldVersion = [defaults integerForKey:@"currentVersion"];
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSInteger nVersion = [version integerValue];
if(bClear == 0 || nVersion > oldVersion)
{
[PGCacheManager clearCacheData:nil];
[defaults setObject:@"1" forKey:@"bCacheClear"];
[defaults setInteger:nVersion forKey:@"currentVersion"];
[defaults synchronize];
}
}
在AppDelegate里面调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//清除缓存
[PGCacheManager clearCacheDataForNewVersion];
......
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
检测版本更新
*/
[PGVersionManager checkVersion];
}