iOS開發之一句代碼檢測APP版本的更新


  • 提示更新效果圖如下,當然也是可以自定義類似與AlertView相似的自定義view,如京東、網易雲音樂都是自定義了這種提示框的view。以下只展示,從App Store獲取到app信息、並解析app信息獲取發布在App Store上的版本號與當前手機里安裝的app版本號做對比,如果有更新就做提示。
  • 在工程中新建一個NSObject類,將以下.h和.m文件中的代碼拷貝至這個新建的類中。
  • //
    //  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
    

      

  • //
    //  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版本更新為例]:
  • //  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
    

      


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM