關於蘋果app版本更新
簡談思路:
當app出現在應用前臺的時候,我們會在應用內檢測當前手機上安裝的app版本edition1,並且同時獲取app store中該app當前版本edition2(xx.xx.xx),對這二個版本做比較(去掉“.”edition換算成整形加減法),如果edition2大於edition1,說明有版本更新,在app store下載自動升級。
程式碼實現:
//
//APPUpdateManager.m
//MoveBand
//
#import "APPUpdateManager.h"
#import <UIKit/UIKit.h>
#import "ChangeLanguage.h"
@interface
//{
//NSString *version; //版本資訊
//NSString *trackViewUrl; //app下載地址
//}
@property (nonatomic,copy) NSString *version;//版本資訊
@property (nonatomic,copy) NSString *trackViewUrl;//app下載地址
@end
@implementation APPUpdateManager
+ (instancetype
staticAPPUpdateManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[APPUpdateManager alloc]init];
});
return manager;
}
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector
}
return self;
}
- (void)appEnterForeground{
NSLog(@"will appEnterForeground");
NSInteger latestCheckTime = [[NSUserDefaultsstandardUserDefaults] integerForKey:@"latestCheckTime"];
NSInteger currentTimeInter = [[NSDate date]timeIntervalSince1970];
if (currentTimeInter -latestCheckTime > 3600 * 24) {
[[NSUserDefaultsstandardUserDefaults]setInteger:currentTimeInter forKey:@"latestCheckTime"];
[[NSUserDefaultsstandardUserDefaults]synchronize];
NSString *urlStr = @"https://itunes.apple.com/lookup?id=1336869336";//1336869336應用的ID
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *req = [NSURLRequestrequestWithURL:url];
[NSURLConnectionconnectionWithRequest:req delegate:self];
}
//1492588093
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSError *error;
id jsonObject = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:&error];
NSDictionary *appInfo = (NSDictionary *)jsonObject;
NSArray *infoContent = [appInfo objectForKey:@"results"];
if (infoContent.count == 0) {
return;
}
self.version = [[infoContent objectAtIndex:0]objectForKey:@"version"];
NSDictionary *infoDic = [[NSBundlemainBundle]infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
//#warning joe 版本號記得是a.a.aa
NSIntegerlocalVersion = [[currentVersion stringByReplacingOccurrencesOfString:@"."withString:@""] integerValue];
NSInteger latestVersion = [[self.versionstringByReplacingOccurrencesOfString:@"."withString:@""] integerValue];
if (localVersion < latestVersion ) {
self.trackViewUrl = [[infoContent objectAtIndex:0]objectForKey:@"trackViewUrl"];
[self alertViewShow];
}
}
- (void)alertViewShow{
UIAlertView *alertview = [[UIAlertViewalloc]initWithTitle:[[ChangeLanguagebundle] localizedStringForKey:@"new_edition"value:@""table:@"English"] message:self.versiondelegate:selfcancelButtonTitle:[[ChangeLanguagebundle] localizedStringForKey:@"Cancel"value:@""table:@"English"] otherButtonTitles: [[ChangeLanguagebundle] localizedStringForKey:@"Sure"value:@""table:@"English"], nil];
[alertview show];//NSLocalizedString(@"IDS_FIND_NEW_DEVICE",@"發現新版本")NSLocalizedString(@"IDS_ENSURE",@"確定")NSLocalizedString(@"IDS_CANCEL",@"取消")
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:self.trackViewUrl]];
}
}
@end