您的位置:首页 > 移动开发 > IOS开发

iOS子线程操作检测版本更新,有新版本通知用户更新, CheckVersion

2015-07-16 14:50 453 查看
iOS子线程操作检测版本更新,有新版本通知用户更新 CheckVersion

一:如何使用:

#import "CheckVersion.h"

//输入你的app在appStore的 id
[CheckVersion check_APP_UPDATE_WITH_APPID:@"350962117"];


上述代码写完就可以了,当用户打开app检测到新版本时,为通知用户,更新,并显示最新版本的更新内容;



二:CheckVersion 类



//
//  CheckVersion.h
//  TopProgressView
//
//  Created by cocoajin on 14-1-20.
//  Copyright (c) 2014年 www.zhgu.net. All rights reserved.
//

#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




//
//  CheckVersion.m
//  TopProgressView
//
//  Created by cocoajin on 14-1-20.
//  Copyright (c) 2014年 www.zhgu.net. All rights reserved.
//

#import "CheckVersion.h"

NSString const *iTnuesApi = @"http://itunes.apple.com/lookup";

#define kTestApp @"http://itunes.apple.com/lookup?id=350962117"  //新浪微博 app测试

@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 = [NSData dataWithContentsOfURL:[NSURL URLWithString:appURLAPI] options:0 error:&dataError];
if (dataError) {
//NSLog(@"appStore app版本信息请求错误!请重新尝试");
[self showAlertWithMessage:@"appStore app版本信息请求错误!请重新尝试"];
return ;
}
JSON = [NSJSONSerialization JSONObjectWithData: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:nil cancelButtonTitle:@"确定" 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 = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"新版本 %@",version] message:releaseNotes delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
[alert show];

#if !__has_feature(objc_arc)
[alert release];
#endif
});
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(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


原文链接:http://www.cnblogs.com/cocoajin/p/3527979.html?utm_source=tuicool
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: