sundaysgarden 2019-02-18 17:44 原文
- 提示更新效果图如下,当然也是可以自定义类似与AlertView相似的自定义view,如京东、网易云音乐都是自定义了这种提示框的view。以下只展示,从App Store获取到app信息、并解析app信息获取发布在App Store上的版本号与当前手机里安装的app版本号做对比,如果有更新就做提示。
![pastedGraphic.png](blob:https://i.cnblogs.com/44249212-c366-4de3-8612-f9ac2a078179)
- 在工程中新建一个NSObject类,将以下.h和.m文件中的代码拷贝至这个新建的类中。
- 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
- //
// HKCheckAppVersionMgr.h
// HKTyy
//
// Created by isHakan on 17/3/24.
// Copyright © 2017年 liuhuakun. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HKCheckAppVersionMgr : NSObject
+ (HKCheckAppVersionMgr *)sharedInstance;
- (void)isUpdataApp:(NSString *)appId;
@end
|
•
|
|
- 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
- //
// HKCheckAppVersionMgr.m
// HKTyy
//
// Created by isHakan on 17/3/24.
// Copyright © 2017年 liuhuakun. All rights reserved.
//
#import "HKCheckAppVersionMgr.h"
#import <UIKit/UIKit.h>
@interface HKCheckAppVersionMgr ()<UIAlertViewDelegate>
@property (nonatomic, strong) NSString *appId;
@end
@implementation HKCheckAppVersionMgr
+ (HKCheckAppVersionMgr *)sharedInstance
{
static HKCheckAppVersionMgr *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[HKCheckAppVersionMgr alloc] init];
});
return instance;
}
- (void)isUpdataApp:(NSString *)appId
{
NSURL *appUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appId]];
NSString *appMsg = [NSString stringWithContentsOfURL:appUrl encoding:NSUTF8StringEncoding error:nil];
NSDictionary *appMsgDict = [self jsonStringToDictionary:appMsg];
NSDictionary *appResultsDict = [appMsgDict[@"results"] lastObject];
NSString *appStoreVersion = appResultsDict[@"version"];
float newVersionFloat = [appStoreVersion floatValue];//新发布的版本号
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
float currentVersionFloat = [currentVersion floatValue];//使用中的版本号
//当前版本小于App Store上的版本&用户未点击不再提示
if (currentVersionFloat<newVersionFloat && ![self isAlertUpdataAgain])
{
self.appId = appId;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"检测到新版本,是否去更新?" delegate:self cancelButtonTitle:@"去更新" otherButtonTitles:@"下次再说",@"不再提示", nil];
[alertView show];
}
}
- (NSDictionary *)jsonStringToDictionary:(NSString *)jsonStr
{
if (jsonStr == nil)
{
return nil;
}
NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&error];
if (error)
{
//NSLog(@"json格式string解析失败:%@",error);
return nil;
}
return dict;
}
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/app/id%@",self.appId]]];
return;
}
if (buttonIndex==2)
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"IS_ALERT_AGAIN"];
[[NSUserDefaults standardUserDefaults] synchronize];
return;
}
}
- (BOOL)isAlertUpdataAgain
{
BOOL res = [[NSUserDefaults standardUserDefaults] objectForKey:@"IS_ALERT_AGAIN"];
return res;
}
@end
|
•
|
|
- 在需要检测App版本是否有更新的地方-[以下是以在ViewController处检测App版本更新为例]:
- 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
- // ViewController.m
// Demo
//
// Created by isHakan on 2017/7/21.
// Copyright © 2017年 liuhuakun. All rights reserved.
//
#import "ViewController.h"
//引入‘头文件’
#import "HKCheckAppVersionMgr.h"
//发布在App Store的Apple ID
#define kYourAppleID @"1234567890"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//一句代码检测更新
[[HKCheckAppVersionMgr sharedInstance] isUpdataApp:kYourAppleID];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|
推荐阅读