• 提示更新效果图如下,当然也是可以自定义类似与AlertView相似的自定义view,如京东、网易云音乐都是自定义了这种提示框的view。以下只展示,从App Store获取到app信息、并解析app信息获取发布在App Store上的版本号与当前手机里安装的app版本号做对比,如果有更新就做提示。
  • 在工程中新建一个NSObject类,将以下.h和.m文件中的代码拷贝至这个新建的类中。
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
  • //
    //  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

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
  • //
    //  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版本更新为例]:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
  • //  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

iOS开发之一句代码检测APP版本的更新的更多相关文章

  1. iOS开发关于Block代码错误

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/4052362.html ,转载请注明出处. iOS开发关于Block代码错误 Incompatible bloc ...

  2. 《iOS开发指南》要改iOS8版本了,听听您的意见?

    <iOS开发指南>要改iOS8版本了,听听您的意见?参加问卷同学均可获得智捷课堂50元代金卡一张,同时抽取一名同学赠送即将出版的基于iOS8的<iOS开发指南>一本,欢迎大家填 ...

  3. 【好程序员笔记分享】——iOS开发之纯代码键盘退出

    -iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之纯代码键盘退出(非常简单)     iOS开发之纯代码键盘退出 前面说到了好几次关于键盘退出的,但 ...

  4. 【转】【iOS开发】打开另一个APP(URL Scheme与openURL)

    目标 平常我们做iOS开发,会经常遇到打开其他的APP的功能.本篇文章讲的就是打开别人的APP的一些知识.我们的目标是: 打开别人的APP 让别人打开我们的APP iOS9的适配问题 使用URL Sc ...

  5. iOS中如何知道app版本已更新

    主要用于程序升级,开启程序后是否显示新特性两个方面. 1.苹果app版本 苹果规定,程序的版本只能升不能降.例如1.0->1.1可以,1.1->1.0就不可以,不允许上架. 2.app版本 ...

  6. IOS开发 统计XCODE 代码行数

    如果要统计ios开发代码,包括头文件的,终端命令进入项目目录下,命令如下 find . -name "*.m" -or -name "*.h" -or -nam ...

  7. iOS开发技巧 -- 复用代码片段

    如果你是一位开发人员在开发过程中会发现有些代码无论是在同一个工程中还是在不同工程中使用率会很高,有经验的人会直接封装在一个类里,或者写成一个宏定义或者把这些代码收集起来,下次直接使用,或者放到xcod ...

  8. Android(2)—Mono For Android App版本自动更新

    0.前言 公司Android查询的项目已经开始,整体采用Java后台+App前台来实现,由于项目小,App这块就我一个人开发,首先需要研究的是:Android版本的更新升级问题:本人经过近一周的学习整 ...

  9. iOS开发笔记10:圆点缩放动画、强制更新、远程推送加语音提醒及UIView截屏

    1.使用CAReplicatorLayer制作等待动画 CALayer+CABasicAnimation可以制作很多简单的动画效果,之前的博客中介绍的“两个动画”,一个是利用一张渐变色图片+CABas ...

随机推荐

  1. SQL Server 锁实验(INSERT加锁探究)

    insert语句: 其上锁情况为: insert语句会对表上的所有索引作出更新,因此这里看到的索引列较多,我们先把所有的索引搞出来看看: 可以看到所有索引都涉及到了,然后我们来仔细分析下加锁情况: 1 ...

  2. February 24th, 2018 Week 8th Saturday

    Those are my principles, and if you don't like them... well, I have others. 那是我的原则,要是你不喜欢......那我还有其 ...

  3. Vue技巧小结(持续更新)

    1. 动态生成的input自动focus 背景: input元素在需要时才插入DOM,这时元素用autofocus属性第一次是可以获取焦点,但是如果有第二个,就不再生效,所以得另外的办法. 方法: / ...

  4. Metasploit渗透测试实际应用

    Metasploit:如何在 Metasploit 中使用反弹 Shell https://xz.aliyun.com/t/2380 Metasploit:如何使用 msfvenom https:// ...

  5. Python开发【第三篇】:函数&读写文件

    三元运算 三元运算,是条件语句的简单的写法.如果条件为真,则返回值1,否则,返回值2. ret = 值1 if 条件 else 值2 深浅拷贝 对于数字(int)和字符串(str)而言,赋值.深拷贝. ...

  6. (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控

    http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...

  7. SpringMVC实现国际化过程中所遇问题

    前言:在利用SpringMVC实现国际化的过程中,看似简单,实则还是遇到了一些小问题,现在笔者对所遇问题总结如下. 注:笔者所用的编辑器为Intellij IEDA 14.1.7版本 1.国际化资源文 ...

  8. UVA10570-Meeting with Aliens(枚举)

    Problem UVA1616-Caravan Robbers Accept: 531  Submit: 2490Time Limit: 3000 mSec Problem Description I ...

  9. 【转】iOS中修改AVPlayer的请求头信息

    在开发中, 我们经常需要在网络请求时修改HTTP/HTTPS的请求头信息 1.普通AFN请求 #import "LMHTTPSessionManager.h" #import &l ...

  10. Sphinx 生成 Windows 帮助文件 (.chm文件)

    本文不介绍 Sphinx 的用法,只简要罗列 Windows 下生成 .chm 文件的步骤. 0. 首先检查机器是否安装了 HTML Help Workshop 软件,一般安装路径应该是 C:\Pro ...