版本更新提醒
- 每當(dāng)版本更新后,都會有一個提醒用戶更新版本的需求,下面這段代碼是為了應(yīng)對這種需求編寫的一個類
使用條件:
- 用到了拿走即用之a(chǎn)fn封裝(OC版)(代碼在本人的簡書中)中的NetworkTools這個類
圖片展示
代碼
#import <Foundation/Foundation.h>
@interface VersionUpdateAlert : NSObject
// 間隔多少次使用河质,再次出現(xiàn)彈框提醒趟脂,默認(rèn)20
@property (nonatomic, assign) NSInteger interCount;
+ (instancetype)shareVersionUpdateAlert;
/**
* appID:appleID
* VC:alertView需要一個控制器引出励稳,一般為設(shè)為rootViewController的那個控制器
*/
- (void)checkAndShowWithAppID:(NSString *)appID andController:(UIViewController *)VC;
@end
#import "VersionUpdateAlert.h"
#import "NetworkTools.h"
@implementation VersionUpdateAlert
+ (instancetype)shareVersionUpdateAlert
{
static VersionUpdateAlert *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[VersionUpdateAlert alloc] init];
});
return instance;
}
- (void)checkAndShowWithAppID:(NSString *)appID andController:(UIViewController *)VC
{
NSString *urlString = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",appID];
[[NetworkTools shareNetworkTools] requestWithMethod:get andUrlString:urlString andParameters:nil andFinished:^(id response, NSError *error) {
if (error == nil) {
NSArray *array = response[@"results"];
NSDictionary *dict = [array lastObject];
NSLog(@"當(dāng)前版本為:%@", dict[@"version"]);
// 獲取info的字典
NSDictionary* infoDict = [NSBundle mainBundle].infoDictionary;
// 版本號保存在本地
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString* appVersion = infoDict[@"CFBundleShortVersionString"];
NSString *versionString = [userDefaults objectForKey:@"versionString"];
if (versionString == nil) {
[userDefaults setObject:appVersion forKey:@"versionString"];
}
//當(dāng)版本更新后重置interCount
if (![[userDefaults objectForKey:@"versionString"] isEqualToString:appVersion]) {
[userDefaults setObject:@"0" forKey:@"interCount"];
[userDefaults setObject:appVersion forKey:@"versionString"];
}
if ([dict[@"version"] compare:appVersion] == NSOrderedDescending) {
NSNumber *interCount = [userDefaults objectForKey:@"interCount"];
if (interCount == nil) {
[self alertShowWithAppID:appID andController:VC];
}
if ([interCount integerValue]%(self.interCount ? self.interCount : 20) == 0) {
[self alertShowWithAppID:appID andController:VC];
}
NSInteger integerInterCount = [interCount integerValue];
integerInterCount++;
NSNumber *numberInterCount = [NSNumber numberWithInteger:integerInterCount];
[userDefaults setObject:numberInterCount forKey:@"interCount"];
}
}
}];
}
//彈出的alertView
- (void)alertShowWithAppID:(NSString *)appID andController:(UIViewController *)VC
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"版本更新提醒" message:@"更新的內(nèi)容,更全面的小說凳谦,更佳的體驗(yàn),快來更新吧" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *installAction = [UIAlertAction actionWithTitle:@"安裝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self openAppaleShopWithAppID:appID];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:installAction];
[alertController addAction:cancelAction];
[VC presentViewController:alertController animated:YES completion:nil];
}
//打開appStore
- (void)openAppaleShopWithAppID:(NSString *)appID
{
NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appID];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
@end
使用
- 在appdelegate finish----方法中調(diào)用
- (void)checkAndShowWithAppID:(NSString *)appID andController:(UIViewController *)VC;
方法
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者