版本更新提醒
- 每当版本更新后,都会有一个提醒用户更新版本的需求,下面这段代码是为了应对这种需求编写的一个类
使用条件:
- 用到了拿走即用之afn封装(OC版)(代码在本人的简书中)中的NetworkTools这个类
图片展示
版本提醒.png
代码
#import <Foundation/Foundation.h>
@interface VersionUpdateAlert : NSObject
// 间隔多少次使用,再次出现弹框提醒,默认20
@property (nonatomic, assign) NSInteger interCount;
+ (instancetype)shareVersionUpdateAlert;
/**
* appID:appleID
* VC:alertView需要一个控制器引出,一般为设为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(@"当前版本为:%@", 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"];
}
//当版本更新后重置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:@"更新的内容,更全面的小说,更佳的体验,快来更新吧" 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----方法中调用
- (void)checkAndShowWithAppID:(NSString *)appID andController:(UIViewController *)VC;
方法
网友评论