ios 版本更新提示-硬更新/软更新
生活随笔
收集整理的這篇文章主要介紹了
ios 版本更新提示-硬更新/软更新
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現:
強制更新:每次彈框
非強制更新:一天提示一次
代碼如下:
步驟一: 將檢測更新寫到APPDelegate的applicationDidBecomeActive中
步驟二: 檢測是否需要更新
步驟三: 針對非強制更新-首先判斷日期如果是同一天的話就不提示更新,如果不是同一天可以提示更新
邏輯如下:前邊是之前的邏輯 簡單解釋一下:0代表未提示更新 1代表已經提示更新 存日期表示將日期存到內存中
前邊是第一次的邏輯,后邊是寫代碼時候的邏輯,更簡化了一些
下邊附上核心代碼:
- (void)compareVersionLocalVerson:(NSString *)localVerson appVerson:(NSString *)appVerson andtype:(NSInteger)type andURl:(NSString *)url{
//將版本號按照.切割后存入數組中
NSArray *localArray = [localVerson componentsSeparatedByString:@"."];
NSArray *appArray = [appVerson componentsSeparatedByString:@"."];
NSInteger minArrayLength = MIN(localArray.count, appArray.count);
BOOL needUpdate = NO;
for(int i=0;i<minArrayLength;i++){//以最短的數組長度為遍歷次數,防止數組越界
//取出每個部分的字符串值,比較數值大小
NSString *localElement = localArray[i];
NSString *appElement = appArray[i];
NSInteger localValue = localElement.integerValue;
NSInteger appValue = appElement.integerValue;
if(localValue<appValue) {
//從前往后比較數字大小,一旦分出大小,跳出循環
needUpdate = YES;
break;
}else if(localValue>appValue){
needUpdate = NO;
break;
}
}
if (needUpdate) {
if (type == 1) {//強制更新
[self showForceUpdate];
}else{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *nowday = [formatter stringFromDate:[NSDate date]];
NSString *saveDay = [UserDefaults objectForKey:@"versionUpdateKey"];
if (saveDay == nil) {
saveDay = @"";
}
if (![saveDay isEqualToString:nowday]) { //假如不是同一天,更新存儲的日期,并且把isHadShowUpdate 設置成yes
[self canChooseUpdate];
[UserDefaults setObject:@"1" forKey:@"isHadShowUpdate"];
[UserDefaults setObject:nowday forKey:@"versionUpdateKey"];
}else{//如果是同一天的話
return;
// if([IsHadShowUpdate isEqualToString:@"0"]){
// [self canChooseUpdate];
// [UserDefaults setObject:@"1" forKey:@"isHadShowUpdate"];
// [UserDefaults setObject:nowday forKey:@"versionUpdateKey"];
// }else{
// return;
// }
}
}
}else{
}
}
非強制更新代碼
//可選更新
-(void)canChooseUpdate{
//彈出提示更新彈框
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"親,有新版本了" message:@"更穩定、快速、多彩的功能和體驗,點擊立即更新!" preferredStyle:UIAlertControllerStyleAlert];
//
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *JumpURL = [[NSUserDefaults standardUserDefaults]objectForKey:@"AppURL"];
if(JumpURL.length ==0){
[JKToast showWithText:@"參數錯誤"];
return;
}else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:JumpURL]];
AppDelegate *app = appDelegate;
UIWindow *window = app.window;
[UIView animateWithDuration:1.0f animations:^{
window.alpha = 0;
window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
} completion:^(BOOL finished) {
exit(0);
}];
}
}];
[alertVc addAction:action2];
[alertVc addAction:action1];
UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController;
[vc presentViewController:alertVc animated:YES completion:nil];
}
p.p1 { margin: 0; font: 20px Menlo; color: rgba(0, 0, 0, 1); background-color: rgba(255, 255, 255, 1) }
對上述的代碼,有任何疑問,可以在下方留言。
也可以給我發郵件咨詢:673658917@qq.com
或者是直接加qq:673658917
轉載請注明出處,謝謝合作。
睡覺舒服,那是給死人準備的,加油吧,一年后你會感謝現在的自己的。
總結
以上是生活随笔為你收集整理的ios 版本更新提示-硬更新/软更新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于表白的句子短句145个
- 下一篇: 王者荣耀游戏网名129个