针对 (X.X)格式
- (void)checkVersionUpdate
{ //当前运行程序的版本信息,可以在 mainBundle 里面获取:
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
// NSLog(@" 本地 %@", infoDict);
NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
CGFloat currentVersionV = [currentVersion floatValue];
NSLog(@" 当前版本 -- %f", currentVersionV);
// 你的应用程序的ID
NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=935947344"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary *paradic = [NSMutableDictionary dictionaryWithObject:@935947344 forKey:@"id"]; //@{@"id":@"935947344"};
[manager POST:urlStr parameters:paradic success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
// NSLog(@" dic = %@", dic);
NSArray *resultsArray = [dic objectForKey:@"results"];
//NSLog(@" results = %@ ", resultsArray);
NSDictionary *resultDic = [resultsArray lastObject];
NSString *version = [resultDic objectForKey:@"version"];
CGFloat newVersionV = [version floatValue];
NSLog(@" appstore version == %f", newVersionV);
NSLog(@" currence version == %f", currentVersionV);
self.trackViewUrl = [resultDic objectForKey:@"trackViewUrl"];
NSString *msg = [NSString stringWithFormat:@"你当前的版本是V%@,发现新版本V%@,是否下载新版本?",currentVersion,version];
//对比发现的新版本和本地的版本
if ( newVersionV < currentVersionV)
{
UIAlertView *createUserResponseAlert = [[UIAlertView alloc] initWithTitle:@"升级提示!" message:msg delegate:self cancelButtonTitle:@"下次再说" otherButtonTitles: @"现在升级", nil];
//[createUserResponseAlert show];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@" 返 错误 %@",error);
}];
}
---------
针对 x.x.x
- (void)execute
{ //自动检查
if (_mode == AutoCheck)
{ //如果已经检查过了,直接结束
if (autoChecked)
{
[self done];
}else{
//如果开关接口获取完成了
if ([SNSwitch isLoadOk]) {
NSDictionary *dic = [GlobalDataCenter defaultCenter].activitySwitchMap;
[self checkVersionUpdateWithSwithMap:dic];
} else {
//add by zhangbeibei:20141016
// [SNSwitch updateWithCallBack:^{
//
// NSDictionary *dic = [GlobalDataCenter defaultCenter].activitySwitchMap;
// [self checkVersionUpdateWithSwithMap:dic];
// }];
NSDictionary *dic = [GlobalDataCenter defaultCenter].activitySwitchMap;
[self checkVersionUpdateWithSwithMap:dic];
}
}
}else //手动检查
{ self.needUpdate = YES;
[SNSwitch updateWithCallBack:^{
NSDictionary *dic = [GlobalDataCenter defaultCenter].activitySwitchMap;
[self checkVersionUpdateWithSwithMap:dic];
}];
}
}
//版本更新算法
//判断是否需要更新的算法
- (BOOL)checkVersionIsNeedUpdateClientV:(NSString *)clientVer
serverV:(NSString *)serverVer
{
if (IsNilOrNull(clientVer) ||
[clientVer isEmptyOrWhitespace] ||
IsNilOrNull(serverVer) ||
[serverVer isEmptyOrWhitespace])
{
return NO;
}
NSArray *clientArray = [clientVer componentsSeparatedByString:@"."];
NSArray *serverArray = [serverVer componentsSeparatedByString:@"."];
NSInteger count =
([clientArray count] > [serverArray count]) ? [clientArray count] : [serverArray count];
for (int i = 0; i < count; i++) {
NSString *clientString = i < [clientArray count] ? [clientArray objectAtIndex:i] : @"0";
NSString *serverString = i < [serverArray count] ? [serverArray objectAtIndex:i] : @"0";
if ([clientString intValue] < [serverString intValue]) {
return YES;
}else if ([clientString intValue] > [serverString intValue]){
return NO;
}
}
return NO;
}
- (BOOL)isNeedForceUpdate:(NSDictionary *)map
{
NSString *forceUpdateVersions = [map objectForKey:kForceUpdateSwithKey];
NSArray *versions = [forceUpdateVersions componentsSeparatedByString:@","];
if (versions && [versions count] > 0) {
NSString *clientVersion =
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
for (NSString *ver in versions)
{
if ([ver isEqualToString:clientVersion]) {
return YES;
}
}
}
return NO;
}
-(BOOL)grayForeUpDate:(NSString*)aStr
{
NSArray *versions = [aStr componentsSeparatedByString:@","];
if (versions && [versions count] > 0) {
NSString *clientVersion =
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
for (NSString *ver in versions)
{
if ([ver isEqualToString:clientVersion]) {
return YES;
}
}
}
return NO;
}
-(NSString*)grayUpDate
{
NSDictionary *dic = [GlobalDataCenter defaultCenter].activitySwitchMap;
if (dic)
{
NSDictionary *updateInfoDic = [dic objectForKey:kGrayUpdateSwithKey];
if (NotNilAndNull(updateInfoDic) && [updateInfoDic isKindOfClass:[NSDictionary class]])
{
NSString *clientVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString *versions = [updateInfoDic objectForKey:kUpdateVersionKey];
NSString *detail = [updateInfoDic objectForKey:kUpdateDetailKey];
if ([self grayForeUpDate:versions]||([clientVersion compare:versions options:NSCaseInsensitiveSearch] == NSOrderedSame))
{
return detail;
}
}
}
return @"-1";
}
//更新提示信息:
- (void)showVersionUpdateDialog:(NSString *)title
content:(NSString *)content
{
BBAlertView *alertView = [[BBAlertView alloc] initWithTitle:IsStrEmpty(title)?L(@"UpdateVersionTip"):title
message:IsStrEmpty(content)?L(@"versionUpdate"):content
delegate:nil
cancelButtonTitle:L(@"LaterUpdate")
otherButtonTitles:L(@"NowUpdate")];
alertView.contentAlignment = UITextAlignmentLeft;
NSString *grayUpdate = [self grayUpDate];
[alertView setConfirmBlock:^{
[SSAIOSSNDataCollection CustomEventCollection:@"click" keyArray: [NSArray arrayWithObjects:@"clickno", nil]valueArray: [NSArray arrayWithObjects:[NSString stringWithFormat:@"1120012"], nil]];
NSURL *iTunesUrl = [NSURL URLWithString:kItunesAppLink];
if (grayUpdate.length && ![grayUpdate isEqualToString:@"-1"])
{
iTunesUrl = [NSURL URLWithString:grayUpdate];
}
[[UIApplication sharedApplication] openURL:iTunesUrl];
if (_mode == AutoCheck)
{
[self done];
}
}];
[alertView setCancelBlock:^{
[SSAIOSSNDataCollection CustomEventCollection:@"click" keyArray: [NSArray arrayWithObjects:@"clickno", nil]valueArray: [NSArray arrayWithObjects:[NSString stringWithFormat:@"1120011"], nil]];
if (_mode == AutoCheck)
{
[self done];
}
}];
[alertView show];
}
//强制更新提示信息:
- (void)showMustVersionUpdateDialog:(NSString *)title content:(NSString *)content
{
BBAlertView *alertView = [[BBAlertView alloc] initWithTitle:IsStrEmpty(title)?L(@"UpdateVersionTip"):title
message:IsStrEmpty(content)?L(@"versionUpdate"):content
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:L(@"Ok")];
alertView.contentAlignment = UITextAlignmentLeft;
alertView.shouldDismissAfterConfirm = NO;
NSString *grayUpdate = [self grayUpDate];
[alertView setConfirmBlock:^{
[SSAIOSSNDataCollection CustomEventCollection:@"click" keyArray: [NSArray arrayWithObjects:@"clickno", nil]valueArray: [NSArray arrayWithObjects:[NSString stringWithFormat:@"1120014"], nil]];
NSURL *iTunesUrl = [NSURL URLWithString:kItunesAppLink];
if (grayUpdate.length && ![grayUpdate isEqualToString:@"-1"])
{
iTunesUrl = [NSURL URLWithString:grayUpdate];
}
[[UIApplication sharedApplication] openURL:iTunesUrl];
}];
[alertView show];
}
- (void)showNoneUpdateDialog
{
self.needUpdate = NO;
BBAlertView *alertView = [[BBAlertView alloc] initWithTitle:nil
message:L(@"MostUpdateVersion")
delegate:nil
cancelButtonTitle:L(@"Ok")
otherButtonTitles:nil];
[alertView show];
}
- (void)checkVersionUpdateWithSwithMap:(NSDictionary *)map
{
if (map == nil || [map count] == 0) {
[self done];
return;
}
NSDictionary *updateInfoDic = [map objectForKey:kSystemVersionKey];
if (NotNilAndNull(updateInfoDic) && [updateInfoDic isKindOfClass:[NSDictionary class]]) {
NSString *serverVersion = [updateInfoDic objectForKey:kUpdateVersionKey];
NSString *clientVersion =
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
//需要更新
if (NotNilAndNull(serverVersion) &&
[self checkVersionIsNeedUpdateClientV:clientVersion serverV:serverVersion])
{
NSString *title = [updateInfoDic objectForKey:kUpdateTitleKey];
NSString *content = [updateInfoDic objectForKey:kUpdateContentKey];
NSString *detail = [updateInfoDic objectForKey:kUpdateDetailKey];
NSString *showContent = nil;
if (!IsStrEmpty(content) && !IsStrEmpty(detail)) {
detail = [detail stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];
showContent = [NSString stringWithFormat:@"%@\n%@", content, detail];
}
if (_mode == ManualCleck)
{
[self done];
}
//是否需要强制更新
if ([self isNeedForceUpdate:map])
{
[SSAIOSSNDataCollection CustomEventCollection:@"click" keyArray: [NSArray arrayWithObjects:@"clickno", nil]valueArray: [NSArray arrayWithObjects:[NSString stringWithFormat:@"121701"], nil]];
[self showMustVersionUpdateDialog:NotNilAndNull(title)?title:nil
content:showContent];
}
else
{
[SSAIOSSNDataCollection CustomEventCollection:@"click" keyArray: [NSArray arrayWithObjects:@"clickno", nil]valueArray: [NSArray arrayWithObjects:[NSString stringWithFormat:@"121701"], nil]];
[self showVersionUpdateDialog:NotNilAndNull(title)?title:nil
content:showContent];
}
}
else //不需要更新
{
if (_mode == ManualCleck)
{ //手动更新
[self showNoneUpdateDialog];
}
[self done];
}
autoChecked = YES;
}
else
{
if (_mode == ManualCleck)
{ //手动更新
[self showNoneUpdateDialog];
}
[self done];
}
}