iOS 版本更新

今天公司要給APP加上“檢查更新”這個服務,因為自己私底下做過類似的功能,所以沒花太多時間就完成了。不過還是看以前公司的代碼完成的,所以覺得還是寫下來比較好,以免以后自己給忘了。
??因為比較簡單,所以下面直接上代碼:

.h里面的代碼

#import <Foundation/Foundation.h>

@interface VersionUpdateObject : NSObject

+(instancetype)sharedInstance;
- (void)updateApp;

@end

.m里面的代碼

#import "VersionUpdateObject.h"
#import "SVProgressHUD.h"

#define kAPP_URL  @"http://itunes.apple.com/lookup?id="
#define kAppID  @"你自己的APPID"

@interface VersionUpdateObject ()
{
    NSTimer *timer;
}
@property(nonatomic,strong)NSString *trackViewUrl;//更新時用到的地址

@end
@implementation VersionUpdateObject

+(instancetype)sharedInstance
{
    static id _versionUpdate;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _versionUpdate = [[self alloc] init];
    });
    return _versionUpdate;
}
#pragma mark - 檢查更新
- (void)updateApp
{
    [SVProgressHUD show];
    if (!timer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(SvProgressDismiss) userInfo:nil repeats:NO];
    }
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", kAPP_URL, kAppID];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //網絡請求
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSError *err;
        
        if (data.length == 0) {
            return;
        }
        NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];

        [self SvProgressDismiss];
        if (err) {
            NSLog(@"%@", err.description);
            return;
        }
        NSArray *resultArray = [appInfoDict objectForKey:@"results"];
        if (![resultArray count]) {
            return;
        }
        
        NSDictionary *infoDict = [resultArray objectAtIndex:0];
        
        //獲取服務器上應用的最新版本號
        NSString *updateVersion = infoDict[@"version"];
        NSString *newVersionStr = [updateVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
        //_trackViewUrl : 更新的時候用到的地址
        _trackViewUrl = infoDict[@"trackViewUrl"];
        
        //獲取當前設備中應用的版本號
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        NSString *oldVersionStr = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
        
//        oldVersionStr = @"1.2";
        //判斷兩個版本是否相同
        if ([oldVersionStr doubleValue] < [newVersionStr doubleValue]) {
            
            NSString *messageStr = @"發現新版本";
            
            dispatch_sync(dispatch_get_main_queue(), ^{
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:messageStr delegate:self cancelButtonTitle:@"暫不更新" otherButtonTitles:@"立即更新", nil];
                alert.tag = [kAppID intValue];
                [alert show];
            });
        }else {  //版本號和app store上的一致
            if (!self.isAutoShowStatus) {//非自動更新
                dispatch_sync(dispatch_get_main_queue(), ^{
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暫無新版本" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
                    alert.tag = [kAppID intValue] + 1;
                    [alert show];
                });
            }
        }
    }];
    [task resume];
}
//判斷用戶點擊了哪一個按鈕
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == [kAppID intValue])
    {
        if (buttonIndex == 1)
        { //點擊”升級“按鈕,就從打開app store上應用的詳情頁面
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.trackViewUrl]];
        }
    }
}

/**
 超過20秒。則取消旋轉
 */
-(void)SvProgressDismiss
{
    NSLog(@"取消菊花旋轉");
    [SVProgressHUD dismiss];
    [timer invalidate];
    timer = nil;
    
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容