ios 提示新版本更新

//
// SHUpdate.m
// shyc
//
// Created by 李凌辉 on 2017/11/28.
// Copyright © 2017年 shyc. All rights reserved.
//

import “SHUpdate.h”

@implementation SHUpdate

// 获取自身版本号
+(NSString *)getLocalVersion{

NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];

return localVersion;

}

// 获取appstore版本号

+(void)getAppStoreVersion{
//定义的App地址
NSString *urlStr = [NSString stringWithFormat:@”http://itunes.apple.com/lookup?id=%@“,AppID];
//AppID即是如图红色箭头获取的AppID
//PS:有的时候可能会请求不到数据,但是AppID对了,有可能是App是上架区域范围的原因,建议使用在com末尾加上“/cn”
//例:NSString *url = [NSString stringWithFormat:@”http://itunes.apple.com/cn/lookup?id=%@“,AppID];
//网络请求App的信息(我们取Version就够了)
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setHTTPMethod:@”POST”];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask task = [session dataTaskWithRequest:request completionHandler:^(NSData _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
if (data) {
//data是有关于App所有的信息
NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([[receiveDic valueForKey:@”resultCount”] intValue]>0) {

            [receiveStatusDic setValue:@"1" forKey:@"status"];
            [receiveStatusDic setValue:[[[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:@"."];
NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];

if ((versionArray.count == 3) && (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];

// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?mt=8",AppID]];
    [[UIApplication sharedApplication]openURL:url];
}];

// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];

[KEYWINDOW.rootViewController presentViewController:alertController animated:YES completion:nil];

}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值