Last active
August 29, 2015 14:12
-
-
Save Na0ki/1e6681b7e9eb4d338001 to your computer and use it in GitHub Desktop.
アプリのバージョンを比較するやつ(Objective-C)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static const int VER_NUM = 3; | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// アプリバージョンを取得 | |
// NSString *currentVer = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; | |
NSString *currentVer = @"0.0.3" //example | |
// 最新のバージョン | |
NSString *latestVer = @"0.10.2" // example | |
// .を区切り文字としてバージョン番号を配列に入れる | |
NSArray *currentVerArray = [self separateVerString:currentVer]; | |
NSArray *latestVerArray = [self separateVerString:latestVer]; | |
// 現在のバージョンとサーバーのバージョンの比較の結果 | |
int comparedVer = [self versionCompare:currentVerArray with:latestVerArray]; | |
if (latestVer != nil && (comparedVer == 0)) { | |
// アラートビューを作成 | |
UIAlertView *updateAlert = [[UIAlertView alloc] initWithTitle:@"更新情報" message:@"最新のバージョンがインストールできます. アップデートをしてください." delegate:self cancelButtonTitle:@"今はしない" otherButtonTitles:@"更新する", nil]; | |
[updateAlert show]; | |
} | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
// バージョン番号を.で分割する | |
- (NSArray *)separateVerString:(NSString *)versionString | |
{ | |
NSMutableString *separate = versionString.mutableCopy; | |
// .で分割 | |
NSArray *array = [separate componentsSeparatedByString:@"."]; | |
// .で分割した結果を配列で返す | |
return array; | |
} | |
- (int)versionCompare:(NSArray *)currentVer with:(NSArray *)latestVer // バージョンが古いと0, それ以外は1を返す | |
{ | |
// currentVerとlatestVerのintegerを入れる配列 | |
int C[VER_NUM]; | |
int L[VER_NUM]; | |
// 配列の結果を格納する変数 | |
int result[VER_NUM]; | |
// 配列内のStringをIntegerに変換し比較 | |
for (int i=0; i<VER_NUM; i++) { | |
// stringをintに変換 | |
C[i] = [currentVer[i] integerValue]; | |
L[i] = [latestVer[i] integerValue]; | |
// 大小比較 | |
if (C[i] < L[i]) { | |
result[i] = 0; | |
} | |
else { | |
result[i] = 1; | |
} | |
} | |
// 高位から順に0または1をみて、0ならば即時return 0する | |
if (result[0] == 0) { | |
return 0; | |
} | |
if (result[1] == 0) { | |
return 0; | |
} | |
if (result[2] == 0) { | |
return 0; | |
} | |
else { | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment