ios 稽查是否有新版本的方法
用的时候需要引入一个文件 CheckUpdate.h 和一个代理。
如下 :#import <UIKit/UIKit.h>
//检查更新用的
#import "CheckUpdate.h"
@interface ViewController : UIViewController<CheckUpdateDelegate>
@end
//目前的是前沿商务的那个id 所以每次都提示更新那个。
// [CheckUpdate shareInstance].delegate = self;
// [[CheckUpdate shareInstance] checkUpdate]; //检查是否最新版本
下面是需要导入文件的代码:
#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"
#import "json.h"
@protocol CheckUpdateDelegate <NSObject>
@optional
- (void)currentVersionHasNewest;
@end
@interface CheckUpdate : NSObject <UIAlertViewDelegate>
@property (assign, nonatomic) id <CheckUpdateDelegate> delegate;
+ (CheckUpdate *)shareInstance;
- (void)checkUpdate;
@end
#import "CheckUpdate.h"
//此APP id为程序申请时得到。更改相应的id查询App的信息
#define kAPPID @"681579701" //商行乐
//应用名字,若需要更改,可自行设置。
#define kAPPName [infoDict objectForKey:@"CFBundleDisplayName"]
//此链接为苹果官方查询App的接口。
#define kAPPURL @"http://itunes.apple.com/lookup?id="
@interfaceCheckUpdate ()
{
NSString *_updateURL;
}
@end
@implementation CheckUpdate
+ (CheckUpdate *)shareInstance
{
static CheckUpdate *update = nil;
if (!update)
{
update = [[CheckUpdate alloc] init];
}
return update;
}
- (void)checkUpdate
{
NSString *urlStr = [NSString stringWithFormat:@"%@%@",kAPPURL, kAPPID];
NSURL *url = [NSURL URLWithString:urlStr];
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url ];
[request setDidFinishSelector:@selector(checkUpdateFinished:)];
[request setDidFailSelector:@selector(checkUpdateFailed:)];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)checkUpdateFinished:(ASIHTTPRequest *)request
{
if (request.responseStatusCode == 200)
{
NSDictionary *infoDict = [[NSBundle mainBundle]infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
NSLog(@"responseString is %@",request.responseString);
NSDictionary *jsonData = request.responseString.JSONValue;
NSArray *infoArray = [jsonData objectForKey:@"results"];
if (infoArray.count >= 1)
{
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
NSString *latestVersion = [releaseInfo objectForKey:@"version"];
NSString *releaseNotes = [releaseInfo objectForKey:@"releaseNotes"];
NSString *title = [NSString stringWithFormat:@"%@ %@版本", kAPPName, latestVersion];
_updateURL = [releaseInfo objectForKey:@"trackViewUrl"];
NSLog(@"_updateURL is %@",_updateURL);
NSLog(@"current is %@,last is %@",currentVersion,latestVersion);
if ([latestVersion compare:currentVersion] == NSOrderedDescending)
{
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:title message:releaseNotes delegate:selfcancelButtonTitle:@"忽略"otherButtonTitles:@"去App Store下载", nil];
[alertView show];
}
else
{
if ([self.delegate respondsToSelector:@selector(currentVersionHasNewest)])
{
[self.delegate currentVersionHasNewest];
}
}
}
else
{
if ([self.delegate respondsToSelector:@selector(currentVersionHasNewest)])
{
[self.delegatecurrentVersionHasNewest];
}
}
}
else
if ([self.delegate respondsToSelector:@selector(currentVersionHasNewest)])
{
[self.delegatecurrentVersionHasNewest];
}
}
- (void)checkUpdateFailed:(ASIHTTPRequest *)request
{
// NSLog(@"data:%@", request.responseString.JSONValue);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:_updateURL]];
}
}
@end