ios實現(xiàn)app強(qiáng)制更新功能

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

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供西盟網(wǎng)站建設(shè)、西盟做網(wǎng)站、西盟網(wǎng)站設(shè)計、西盟網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、西盟企業(yè)網(wǎng)站模板建站服務(wù),十余年西盟做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

效果圖:

ios實現(xiàn)app強(qiáng)制更新功能

在appdelegate里面寫下面代碼

  //提示版本更新
 [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:@"現(xiàn)在升級" 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從后臺切到前臺時調(diào)用的方法
- (void)applicationDidBecomeActive:(UIApplication * )application
{
  [self VersonUpdate];
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文標(biāo)題:ios實現(xiàn)app強(qiáng)制更新功能
網(wǎng)頁鏈接:http://muchs.cn/article36/iiopsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、品牌網(wǎng)站制作、面包屑導(dǎo)航、網(wǎng)站策劃動態(tài)網(wǎng)站、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)