第一种,手动检查

//
//  Harpy.h
//  Harpy
//
//  Created by Arthur Ariel Sabintsev on 11/14/12.
//  Copyright (c) 2012 Arthur Ariel Sabintsev. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Harpy : NSObject <UIAlertViewDelegate>

/*
  Checks the installed version of your application against the version currently available on the iTunes store.
  If a newer version exists in the AppStore, it prompts the user to update your app.
 */
+ (void)checkVersion;

@end

//
//  Harpy.m
//  Harpy
//
//  Created by Arthur Ariel Sabintsev on 11/14/12.
//  Copyright (c) 2012 Arthur Ariel Sabintsev. All rights reserved.
//

#define kHarpyCurrentVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey]

@interface Harpy ()

+ (void)showAlertWithAppStoreVersion:(NSString*)appStoreVersion;

@end

@implementation Harpy

#pragma mark - Public Methods
+ (void)checkVersion
{

// Asynchronously query iTunes AppStore for publically available version
    NSString *storeString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", kHarpyAppID];
    NSURL *storeURL = [NSURL URLWithString:storeString];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:storeURL];
    [request setHTTPMethod:@"GET"];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
       
        if ( [data length] > 0 && !error ) { // Success
            
            NSDictionary *appData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

dispatch_async(dispatch_get_main_queue(), ^{
                
                // All versions that have been uploaded to the AppStore
                NSArray *versionsInAppStore = [[appData valueForKey:@"results"] valueForKey:@"version"];
                
                if ( ![versionsInAppStore count] ) { // No versions of app in AppStore
                    
                    return;
                    
                } else {

NSString *currentAppStoreVersion = [versionsInAppStore objectAtIndex:0];

if ([kHarpyCurrentVersion compare:currentAppStoreVersion options:NSNumericSearch] == NSOrderedAscending) {
                        
                        //[Harpy showAlertWithAppStoreVersion:currentAppStoreVersion];
                    
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"checkCurrentVersion" object:currentAppStoreVersion];
                    }
                    else {
                    
                        // Current installed version is the newest public version or newer    
                        
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"checkCurrentVersion" object:@""];

}

}
              
            });
        }
        
    }];
}

#pragma mark - Private Methods
+ (void)showAlertWithAppStoreVersion:(NSString *)currentAppStoreVersion
{
    
    NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
    
    if ( harpyForceUpdate ) { // Force user to update app
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:kHarpyAlertViewTitle
                                                            message:[NSString stringWithFormat:@"新版本 %@ 可用. 请更新至版本 %@ .", appName, currentAppStoreVersion]
                                                           delegate:self
                                                  cancelButtonTitle:kHarpyUpdateButtonTitle
                                                  otherButtonTitles:nil, nil];
        
        [alertView show];
        
    } else { // Allow user option to update next time user launches your app
        
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:kHarpyAlertViewTitle
                                                            message:[NSString stringWithFormat:@"新版本 %@ 可用. 请更新至版本 %@ .", appName, currentAppStoreVersion]
                                                           delegate:self
                                                  cancelButtonTitle:kHarpyCancelButtonTitle
                                                  otherButtonTitles:kHarpyUpdateButtonTitle, nil];
        
        [alertView show];
        
    }
    
}

#pragma mark - UIAlertViewDelegate Methods
+ (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if ( harpyForceUpdate ) {

NSString *iTunesString = [NSString stringWithFormat:@"https://itunes.apple.com/app/id%@", kHarpyAppID];
        NSURL *iTunesURL = [NSURL URLWithString:iTunesString];
        [[UIApplication sharedApplication] openURL:iTunesURL];
        
    } else {

switch ( buttonIndex ) {
                
            case 0:{ // Cancel / Not now
        
                // Do nothing
                
            } break;
                
            case 1:{ // Update
                
                NSString *iTunesString = [NSString stringWithFormat:@"https://itunes.apple.com/app/id%@", kHarpyAppID];
                NSURL *iTunesURL = [NSURL URLWithString:iTunesString];
                [[UIApplication sharedApplication] openURL:iTunesURL];
                
            } break;
                
            default:
                break;
        }
        
    }

}

@end

//
//  HarpyConstants.h
//  
//
//  Created by Arthur Ariel Sabintsev on 1/30/13.
//
//

/*
 Option 1 (DEFAULT): NO gives user option to update during next session launch
 Option 2: YES forces user to update app on launch
 */
static BOOL harpyForceUpdate = NO;

// 2. Your AppID (found in iTunes Connect)
#define kHarpyAppID                 @"807432039" //这里是已经发布的app id在itunes connect中可以查询到

// 3. Customize the alert title and action buttons
#define kHarpyAlertViewTitle        @"有可用更新"
#define kHarpyCancelButtonTitle     @"取消"
#define kHarpyUpdateButtonTitle     @"更新"

//得到appid如果有新的版本就真正到页面去做更新

- (void)startUpdateVersion
{
    NSString *iTunesString = [NSString stringWithFormat:@"https://itunes.apple.com/app/id%@", kHarpyAppID];
    NSURL *iTunesURL = [NSURL URLWithString:iTunesString];
    [[UIApplication sharedApplication] openURL:iTunesURL];

}

第二种自动在屏幕顶部的通知栏中提示有更新

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

[[AWVersionAgent sharedAgent] setDebug:YES];
    [[AWVersionAgent sharedAgent] checkNewVersionForApp:kHarpyAppID];

}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{

[[AWVersionAgent sharedAgent] upgradeAppWithNotification:notification];

}

//
//  AWVersionAgent.h
//  AWVersionAgent
//
//  Created by Heyward Fann on 1/31/13.
//  Copyright (c) 2013 Appwill. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface AWVersionAgent : NSObject

+ (AWVersionAgent *)sharedAgent;

@property (nonatomic) BOOL debug;

- (void)checkNewVersionForApp:(NSString *)appid;
- (void)upgradeAppWithNotification:(UILocalNotification *)notification;

@end

//
//  AWVersionAgent.m
//  AWVersionAgent
//
//  Created by Heyward Fann on 1/31/13.
//  Copyright (c) 2013 Appwill. All rights reserved.
//

#import "AWVersionAgent.h"

#define kAppleLookupURLTemplate     @"http://itunes.apple.com/lookup?id=%@"
#define kAppStoreURLTemplate        @"itms-apps://itunes.apple.com/app/id%@"

#define kUpgradeAlertMessage    @"新版本可用, 当前版本: %@, 新版本: %@. 现在从App Store即可更新至新版本."
#define kUpgradeAlertAction     @"kUpgradeAlertAction"
#define kUpgradeAlertDelay      3

#define kAWVersionAgentLastNotificationDateKey      @"lastNotificationDate"
#define kAWVersionAgentLastCheckVersionDateKey      @"lastCheckVersionDate"

@interface AWVersionAgent ()

@property (nonatomic, copy) NSString *appid;
@property (nonatomic) BOOL newVersionAvailable;

@end

@implementation AWVersionAgent

+ (AWVersionAgent *)sharedAgent
{
    static AWVersionAgent *sharedAgent = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedAgent = [[AWVersionAgent alloc] init];
    });
    
    return sharedAgent;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (id)init
{
    self = [super init];
    if (self) {
        _newVersionAvailable = NO;
        _debug = NO;
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(showUpgradeNotification)
                                                     name:UIApplicationDidEnterBackgroundNotification
                                                   object:nil];
    }
    
    return self;
}

- (void)checkNewVersionForApp:(NSString *)appid
{
    self.appid = appid;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *url = [NSString stringWithFormat:kAppleLookupURLTemplate, _appid];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
        if (data && [data length]>0) {
            id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
            if (obj && [obj isKindOfClass:[NSDictionary class]]) {
                NSDictionary *dict = (NSDictionary *)obj;
                NSArray *array = dict[@"results"];
                if (array && [array count]>0) {
                    NSDictionary *app = array[0];
                    NSString *newVersion = app[@"version"];
                    [[NSUserDefaults standardUserDefaults] setObject:newVersion
                                                              forKey:@"kAppNewVersion"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    NSString *curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
                    if (newVersion && curVersion && ![newVersion isEqualToString:curVersion]) {
                        self.newVersionAvailable = YES;
                    }
                }
            }
        }
    });
}

- (BOOL)conditionHasBeenMet
{
    if (_debug) {
        return YES;
    }
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSTimeInterval last = [defaults doubleForKey:kAWVersionAgentLastNotificationDateKey];
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    if (last <= 0) {
        [defaults setDouble:now forKey:kAWVersionAgentLastNotificationDateKey];
        [defaults synchronize];
        
        return NO;
    }
    if (now - last < 60*60*24) {
        return NO;
    }
    
    return _newVersionAvailable;
}

- (void)showUpgradeNotification
{
    if ([self conditionHasBeenMet]) {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.fireDate = [[NSDate date] dateByAddingTimeInterval:kUpgradeAlertDelay];
        notification.timeZone = [NSTimeZone defaultTimeZone];
        NSString *curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
        NSString *newVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"kAppNewVersion"];
        NSString *msg = [NSString stringWithFormat:kUpgradeAlertMessage,
                         curVersion, newVersion];
        notification.alertBody = msg;
        notification.alertAction = kUpgradeAlertAction;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        
        [[NSUserDefaults standardUserDefaults] setDouble:[[NSDate date] timeIntervalSince1970]
                                                  forKey:kAWVersionAgentLastNotificationDateKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (void)upgradeAppWithNotification:(UILocalNotification *)notification
{
    if ([notification.alertAction isEqualToString:kUpgradeAlertAction]) {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
        NSString *url = [NSString stringWithFormat:kAppStoreURLTemplate, _appid];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        
        self.newVersionAvailable = NO;
    }
}

@end

app 检查更新和更新的更多相关文章

  1. Bugly实现app全量更新

    转 http://blog.csdn.net/qq_33689414/article/details/54911895Bugly实现app全量更新 Bugly官网文档 一.参数配置 在app下的gra ...

  2. Android实现App版本自动更新

    现在很多的App中都会有一个检查版本的功能.例如斗鱼TV App的设置界面下: 当我们点击检查更新的时候,就会向服务器发起版本检测的请求.一般的处理方式是:服务器返回的App版本与当前手机安装的版本号 ...

  3. Cordova app 检查更新 ----JS进行调用(二)

    原文:Cordova app 检查更新 ----JS进行调用(二) 1.获取版本号 需要添加 插件 cordova plugin add https://github.com/whiteoctober ...

  4. cordova APP 检查更新

    原文:cordova APP 检查更新 //升级程序 .factory('UpdateService', function ($rootScope, $cordovaAppVersion, $cord ...

  5. Cordova app 检查更新 ----创建项目、添加插件、修改插件(一)

    原文:Cordova app 检查更新 ----创建项目.添加插件.修改插件(一) 使用Cordova 进行跨平台应用程序的开发 1.创建Cordova项目 $ cordova create hell ...

  6. 如何实现已发布app的自动更新

    要实现app的自动更新,做两件事情就可以搞定 1.获取当前手机中的app版本号 我们可以通过查询mainbundle中的获取CFBundleVersion NSDictionary *infoDict ...

  7. 创建APP检查更新页

    本文来源及参考:Create a check for updates page for your app. 这篇文章解释了如何创建一个简单的检查更新页,检查该用户已安装的应用程序的最新版本. 简介 这 ...

  8. 最全APP安装/卸载/更新测试点

    1.安装查看在安装过程中存在的提示信息是否明确,意思是否明确在安装过程中,点击取消按钮,能否正常退出安装程序,软件是否可用.安装时是否识别有SD卡,并默认安装到sd卡中安装过程中,接听电话或者短信,安 ...

  9. HBuilderX开发app实现自动更新版本

      需求说明:使用MUI+Vue等技术并且通过HBuilderX打包开发移动app,在有版本更新时需要自动提示用户有新版本,并且可以点击下载自动安装. 思路说明: 应用打开时(使用Vue的生命周期mo ...

随机推荐

  1. 12864点阵液晶显示模块的原理和实例程序(HJ12864M-1)

    12864点阵液晶显示模块(LCM)就是由 128*64个液晶显示点组成的一个128列*64行的阵列.每个显示点对应一位二进制数,1表示亮,0表示灭.存储这些点阵信息的RAM称为显示数据存 储器.要显 ...

  2. 多实例设置本地IP访问sql server 数据库

    我们本地有时候有多个数据库版本(^_^..别说了都是泪),都是为了兼容不同版本的数据而安装的! 最近我们需要用IP来访问,就有了这一段折腾的历程. 上图片为我安装的三个不同的版本,一个为sql ser ...

  3. ogre3D学习基础8 --- 资源管理器

    资源管理 可管理的资源有: 材质资源:在.material文件中包含的材质脚本定义(技术.通路.纹理单元等数据的定义). 模型资源:经过优化的二进制网格模型文件,扩展名为.mesh.包含几何信息和一些 ...

  4. 计算n的阶乘(n!)末尾0的个数

    题目: 给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数. 举例: 5!=120,其末尾所含有的“0”的个数为1: 10!= 3628800,其末尾所含有的“0”的个数为2: 20!= 24 ...

  5. SDOJ 1195 Zhenhuan

    描述 今日又在看甄嬛传,皇上觉得后宫们的勾心斗角太险恶了,有点不好,决定给每个妃子发丝带以让后宫之间和睦相处.皇上一共有N个后宫(标号为1~n),站成一个环形(1号与n号相邻),每个后宫想要ai个丝带 ...

  6. ccna学习指南第七版

    1.加电post自检    闪存查找ios 可随时从命令行进入设置模式,为此可在特权模式下输入setup    ctrl+c退出特权模式 6.2cli   命令行界面 进入cli router> ...

  7. 对python的想法

    作为计算机专业的学生,在编程语言之余,我认为掌握一门脚本语言是很必要的.尤其是现在在数据分析,AI,机器学习等各个方面都大放异彩的python.相比于之前接触过的Java,C,C++乃至于php等语言 ...

  8. AtCoder Regular Contest 091

    数学场,做到怀疑人生系列 C - Flip,Flip, and Flip...... Time limit : 2sec / Memory limit : 256MB Score : 300 poin ...

  9. Unity3D - UGUI的初级应用

    添加字体: 把下载好的字体拖拽到Project面板中 - 点击Text组件中Text属性后面的圆点 - 选择刚刚拖拽的字体即可. 创建ToggleGroup(开关组): 1.在Canvas下创建两个T ...

  10. iOS学习笔记28-系统服务(一)短信和邮件

    一.系统应用 在开发某些应用时,我们可能希望能够调用iOS系统内置的电话.短信.邮件.浏览器应用,或者直接调用安装的第三方应用,这个要怎么实现呢? 这里统一使用UIApplication的一个对象方法 ...