最近因項(xiàng)目需求,需要用到強(qiáng)制更新功能,網(wǎng)上搜了一下,挺多的,但是把網(wǎng)上的代碼拷貝以后,發(fā)現(xiàn)一個(gè)bug,就是進(jìn)app,彈出框顯示,點(diǎn)擊現(xiàn)在升級(jí),跳轉(zhuǎn)到AppStore下載里面,但是我不下載,又切回到app里面,發(fā)現(xiàn)彈出框就不跳了,其實(shí)也簡(jiǎn)單,就是appdelegate里面有個(gè)代理方法,就是當(dāng)app從后臺(tái)切到前臺(tái)走的方法,將強(qiáng)制更新方法在這里面在調(diào)用一下就行了~~~話(huà)不多說(shuō),上代碼!!!用的話(huà)直接粘貼復(fù)制~

效果圖:

在appdelegate里面寫(xiě)下面代碼
//提示版本更新
[self VersonUpdate];
#pragma mark ------提示用戶(hù)版本更新------
-(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自身版本號(hào)
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:@"版本過(guò)低,需要升級(jí)到最新版本"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升級(jí)提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"現(xiàn)在升級(jí)" 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];
}
//當(dāng)app從后臺(tái)切到前臺(tái)時(shí)調(diào)用的方法
- (void)applicationDidBecomeActive:(UIApplication * )application
{
[self VersonUpdate];
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前標(biāo)題:ios實(shí)現(xiàn)app強(qiáng)制更新功能-創(chuàng)新互聯(lián)
標(biāo)題URL:http://chinadenli.net/article14/hdhge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站內(nèi)鏈、網(wǎng)站營(yíng)銷(xiāo)、外貿(mào)建站、商城網(wǎng)站、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容