HarpyConstants.h
static BOOL harpyForceUpdate = NO;
#define kHarpyAppID @"573293275"
#define kHarpyAlertViewTitle @"Update Available"
#define kHarpyCancelButtonTitle @"稍后更新"
#define kHarpyUpdateButtonTitle @"立即更新"
Harpy.h
#import <Foundation/Foundation.h>
@interface Harpy : NSObject<UIAlertViewDelegate>
+ (void)checkVersion;
@end
Harpy.m
#import "Harpy.h"
#import "HarpyConstants.h"
#define kHarpyCurrentVersion [[[NSBundle mainBundle] infoDictionary]objectForKey:(NSString*)kCFBundleVersionKey]
@interface Harpy ()
+(void)showAlertWithAppStoreVersion:(NSString*)appStoreVersion;
@end
@implementation Harpy
#pragma mark - Public Methods
+ (void)checkVersion
{
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:queuecompletionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ( [data length] > 0 && !error ) { // Success
NSDictionary *appData = [NSJSONSerialization JSONObjectWithData:dataoptions:NSJSONReadingAllowFragments error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *versionsInAppStore =[[appData valueForKey:@"results"] valueForKey:@"version"];
if ( ![versionsInAppStorecount] ) { // No versions of app in AppStore
return;
} else {
NSString*currentAppStoreVersion = [versionsInAppStore objectAtIndex:0];
if ([kHarpyCurrentVersion compare:currentAppStoreVersion options:NSNumericSearch] == NSOrderedAscending){
[Harpy showAlertWithAppStoreVersion:currentAppStoreVersion];
}
else {
// Current installedversion is the newest public version or newer
}
}
});
}
}];
}
#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:@"A new version of %@ isavailable. Please update to version %@ now.", 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:@"A new version of %@ isavailable. Please update to version %@ now.", appName,currentAppStoreVersion]
delegate:self
cancelButtonTitle:kHarpyCancelButtonTitle
otherButtonTitles:kHarpyUpdateButtonTitle, nil];
[alertView show];
}
}
#pragma mark - UIAlertViewDelegate Methods
+ (void)alertView:(UIAlertView *)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex
{
if( harpyForceUpdate ) {
NSString *iTunesString = [NSStringstringWithFormat:@"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
iOS子线程操作检测版本更新,有新版本通知用户更新 CheckVersion
一:如何使用:
#import"CheckVersion.h"
//输入你的app在appStore的 id
[CheckVersion check_APP_UPDATE_WITH_APPID:@"350962117"];
上述代码写完就可以了,当用户打开app检测到新版本时,为通知用户,更新,并显示最新版本的更新内容;
二:CheckVersion 类
#import<Foundation/Foundation.h>
extern NSString const *iTnuesApi;
@interface CheckVersion : NSObject
//+ (instancetype)check;
+ (NSString *)check_LocalApp_Version;
+ (void)check_APP_UPDATE_WITH_APPID:(NSString *)appid;
@end
View Code
// CheckVersion.m
#import"CheckVersion.h"
NSString const *iTnuesApi = @"http://itunes.apple.com/lookup";
#define kTestApp @"http://itunes.apple.com/lookup?id=350962117"
@implementation CheckVersion
+ (instancetype)check
{
static CheckVersion *check = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
check = [[CheckVersion alloc]init];
});
return check;
}
+ (NSString *)check_LocalApp_Version;
{
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];
return localVersion;
}
+ (void)check_APP_UPDATE_WITH_APPID:(NSString *)appid
{
__block id JSON = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSError *dataError = nil;
NSString *appURLAPI = [NSString stringWithFormat:@"%@?id=%@",iTnuesApi,appid];
NSData *appData = [NSDatadataWithContentsOfURL:[NSURL URLWithString:appURLAPI] options:0 error:&dataError];
if (dataError) {
//NSLog(@"appStore app版本信息请求错误!请重新尝试");
[self showAlertWithMessage:@"appStore app版本信息请求错误!请重新尝试"];
return ;
}
JSON = [NSJSONSerializationJSONObjectWithData:appData options:0 error:nil];
//NSLog(@"ddd : %@",JSON);
if ([[JSON objectForKey:@"resultCount"] intValue] > 0) {
NSString *remoteVersion = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"version"];
NSString *releaseNotes = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"releaseNotes"];
NSString *trackURL = [[[JSON objectForKey:@"results"] objectAtIndex:0] objectForKey:@"trackViewUrl"];
[[NSUserDefaults standardUserDefaults]setObject:trackURL forKey:@"KK_THE_APP_UPDATE_URL"];
//NSLog(@"%@ %@%@",remoteVersion,releaseNotes,trackURL);
NSString *localVersion = [self check_LocalApp_Version];
if ([remoteVersion floatValue] > [localVersion floatValue]) {
[[CheckVersion check]newVersionUpdate:remoteVersion notes:releaseNotes];
}
else
{
return;
}
}
else
{
//NSLog(@"appStore 无app信息,请检查您的 app id");
[self showAlertWithMessage:@"appStore 无此app信息,请检查您的 app id"];
return ;
}
});
}
+ (void)showAlertWithMessage:(NSString*)messages
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"版本更新提示" message:messages delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil, nil];
[alert show];
#if !__has_feature(objc_arc)
[alert release];
#endif
});
}
- (void)newVersionUpdate:(NSString*)version notes:(NSString *)releaseNotes
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:[NSString stringWithFormat:@"新版本 %@",version] message:releaseNotes delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"更新", nil];
[alert show];
#if !__has_feature(objc_arc)
[alert release];
#endif
});
}
- (void)alertView:(UIAlertView*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==1) {
//NSString *apiUrl = @"https://itunes.apple.com/us/app/wei-bo/id350962117?mt=8&uo=4";
//apiUrl =@"itms-apps://itunes.apple.com/cn/app/wei-bo/id350962117?mt=8";
NSString *theAppURL = [[NSUserDefaults standardUserDefaults]objectForKey:@"KK_THE_APP_UPDATE_URL"];
NSURL *appStoreURL = [NSURL URLWithString:theAppURL];
[[UIApplication sharedApplication]openURL:appStoreURL];
}
}
@end