版本更新管理
单例封装了一个版本更新管理类
UI效果:
+ (instancetype)sharedInstance {
static UpdateVersionManage *sharedInstance =nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (void)setIsHaveIgnoreUP:(BOOL)isHaveIgnoreUP{
_isHaveIgnoreUP = isHaveIgnoreUP;
}
-(instancetype)init{
self=[superinit];
if (self) {
__weak typeof(self) weakSelf=self;
self.updateBlock = ^(NSString *currentVersion ,NSString *releaseNotes,NSString *trackUrl, NSString *type){
__strong typeof(self) strongSelf=weakSelf;
[strongSelf showUpdateViewWith:(NSString *)currentVersionwith:(NSString *)releaseNoteswith:(NSString *)trackUrlwith:type];
};
}
return self;
}
#pragma mark--版本更新--1表示不需要更新,2选择更新,3强制更新
+(void)checkVersionInController:(UIViewController *)showController{
#pragma mark--type --网络请求获得type值,根据业务处理
NSString *type = @"3";
if ([typeisEqualToString:@"2"])
{//有更新,当前版本可用
if ([UpdateVersionManagesharedInstance].isHaveIgnoreUP ==YES) {
return ;
}
[UpdateVersionManagesharedInstance].showController = showController;
[UpdateVersionManagesharedInstance].isHaveIgnoreUP =YES;
if ([UpdateVersionManagesharedInstance].updateBlock) {
[UpdateVersionManagesharedInstance].updateBlock(@"更新内容",@"优化了哪些内容",@"下载链接",type);
}
}
else if ([typeisEqualToString:@"3"])//强制更新
{
[UpdateVersionManagesharedInstance].showController = showController;
if ([UpdateVersionManagesharedInstance].updateBlock) {
[UpdateVersionManagesharedInstance].updateBlock(@"更新内容",@"优化了哪些内容",@"下载链接",type);
}
}
}
#pragma mark - 更新界面
-(void)showUpdateViewWith:(NSString *)currentVersion with:(NSString *)releaseNotes with:(NSString *)trackUrl with:(NSString *)type{
if ([typeisEqualToString:@"2"])
{
[self showIgnoreUpdateViewWith:currentVersionwith:releaseNotes with:trackUrl];
}else if ([typeisEqualToString:@"3"])
{
[self showMustUpdateViewWith:currentVersionwith:releaseNotes with:trackUrl];
}
}
-(void)showIgnoreUpdateViewWith:(NSString *)currentVersion with:(NSString *)releaseNotes with:(NSString *)trackUrl{
UIAlertController *alertController=[UIAlertControlleralertControllerWithTitle:[NSStringstringWithFormat:@"发现新版本%@",currentVersion] message:releaseNotespreferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1=[UIAlertActionactionWithTitle:@"下次再说" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
}];
UIAlertAction *action2=[UIAlertActionactionWithTitle:@"前往更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:[trackUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]]]];
}];
[alertController addAction:action1];
[alertController addAction:action2];
if ([UpdateVersionManagesharedInstance].showController) {
[[UpdateVersionManagesharedInstance].showControllerpresentViewController:alertController animated:YEScompletion:nil];
}else
{
[[UpdateVersionManagegetCurrentVC]presentViewController:alertController animated:YEScompletion:nil];
}
}
-(void)showMustUpdateViewWith:(NSString *)currentVersion with:(NSString *)releaseNotes with:(NSString *)trackUrl{
UIAlertController *alertController=[UIAlertControlleralertControllerWithTitle:[NSStringstringWithFormat:@"发现新版本%@",currentVersion] message:releaseNotespreferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action2=[UIAlertActionactionWithTitle:@"前往更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:[trackUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]]]];
}];
[alertController addAction:action2];
if ([UpdateVersionManagesharedInstance].showController) {
[[UpdateVersionManagesharedInstance].showControllerpresentViewController:alertController animated:YEScompletion:nil];
}else
{
[[UpdateVersionManagegetCurrentVC]presentViewController:alertController animated:YEScompletion:nil];
}
}
Git地址demo