最近因项目需求,需要用到强制更新功能,网上搜了一下,挺多的,但是把网上的代码拷贝以后,发现一个bug,就是进app,弹出框显示,点击现在升级,跳转到appstore下载里面,但是我不下载,又切回到app里面,发现弹出框就不跳了,其实也简单,就是appdelegate里面有个代理方法,就是当app从后台切到前台走的方法,将强制更新方法在这里面在调用一下就行了~~~话不多说,上代码!!!用的话直接粘贴复制~
效果图:
在appdelegate里面写下面代码
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
|
//提示版本更新
[self versonupdate];
#pragma mark ------提示用户版本更新------
-( void )versonupdate{
//定义app地址
nsstring *urld = [nsstring stringwithformat:@ "http://itunes.apple.com/lookup?id=%d" ,1178114725];
nsurl *url = [nsurl urlwithstring:urld];
nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringcachedata timeoutinterval:10];
[request sethttpmethod:@ "post" ];
nsurlsession *session = [nsurlsession sharedsession];
nsurlsessiondatatask *task = [session datataskwithurl:url completionhandler:^(nsdata * _nullable data, nsurlresponse * _nullable response, nserror * _nullable error) {
nslog(@ "%@" ,response);
nsmutabledictionary *receivestatusdic = [[nsmutabledictionary alloc]init];
if (data) {
nsdictionary *receivedic = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves error:nil];
if ([[receivedic valueforkey:@ "resultcount" ] intvalue] > 0) {
[receivestatusdic setobject:@ "1" forkey:@ "status" ];
[receivestatusdic setobject:[[[receivedic valueforkey:@ "results" ] objectatindex:0] valueforkey:@ "version" ] forkey:@ "version" ];
[self performselectoronmainthread:@selector(receivedata:) withobject:receivestatusdic waituntildone:no];
} else {
[receivestatusdic setvalue:@ "1" forkey:@ "status" ];
}
} else {
[receivestatusdic setvalue:@ "-1" forkey:@ "status" ];
}
}];
[task resume];
}
-( void )receivedata:(id)sender
{
//获取app自身版本号
nsstring *localversion = [[[nsbundle mainbundle]infodictionary]objectforkey:@ "cfbundleshortversionstring" ];
nsarray *localarray = [localversion componentsseparatedbystring:@ "." ]; //1.0
nsarray *versionarray = [sender[@ "version" ] componentsseparatedbystring:@ "." ]; //3 2.1.1
// if ((versionarray.count == 2) && (localarray.count == versionarray.count)) {
if ([localarray[0] intvalue] < [versionarray[0] intvalue]) {
[self updateversion];
} else if ([localarray[0] intvalue] == [versionarray[0] intvalue]){
if ([localarray[1] intvalue] < [versionarray[1] intvalue]) {
[self updateversion];
} else if ([localarray[1] intvalue] == [versionarray[1] intvalue]){
if ([localarray[2] intvalue] < [versionarray[2] intvalue]) {
[self updateversion];
}
}
}
// }
}
-( void )updateversion{
nsstring *msg = [nsstring stringwithformat:@ "版本过低,需要升级到最新版本" ];
uialertcontroller *alertcontroller = [uialertcontroller alertcontrollerwithtitle:@ "升级提示" message:msg preferredstyle:uialertcontrollerstylealert];
uialertaction *otheraction = [uialertaction actionwithtitle:@ "现在升级" style:uialertactionstyledestructive handler:^(uialertaction*action) {
nsurl *url = [nsurl urlwithstring:[nsstring stringwithformat:@ "https://itunes.apple.com/cn/app/m-help/id1178114725?mt=8" ]];
[[uiapplication sharedapplication]openurl:url];
}];
[alertcontroller addaction:otheraction];
[self.window.rootviewcontroller presentviewcontroller:alertcontroller animated:yes completion:nil];
}
//当app从后台切到前台时调用的方法
- ( void )applicationdidbecomeactive:(uiapplication * )application
{
[self versonupdate];
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.jianshu.com/p/700505ce2643?utm_source=tuicool&utm_medium=referral#