Skip to content

Commit 464f102

Browse files
Use proper numeric version comparison
1 parent 1aea42f commit 464f102

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

AppBox/Common/UpdateHandler/UpdateHandler.m

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,30 @@ + (void)isNewVersionAvailableCompletion:(void (^)(bool available, NSURL *url))co
3535
[NetworkHandler requestWithURL:abGitHubLatestRelease withParameters:nil andRequestType:RequestGET andCompletetion:^(id responseObj, NSInteger httpStatus, NSError *error) {
3636
//handle error and check for all required keys
3737
@try {
38-
if (error == nil &&
38+
if (error == nil && [responseObj isKindOfClass:[NSDictionary class]] &&
3939
[((NSDictionary *)responseObj).allKeys containsObject:@"tag_name"] &&
4040
[((NSDictionary *)responseObj).allKeys containsObject:@"html_url"]){
4141

4242
//get tag name, because it's always be latest version
4343
NSString *tag = [responseObj valueForKey:@"tag_name"];
44-
NSString *newVesion = [[tag componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:abEmptyString];
4544

4645
//get version string from bundle info.plist
4746
NSString *versionString = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];
48-
NSString *currentVersion = [[versionString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:abEmptyString];
47+
if (!versionString || !tag) {
48+
completion(false, nil);
49+
return;
50+
}
4951

5052
//log current and latest version
5153
DDLogDebug(@"Current Version - %@ <=> Latest Version - %@", versionString, tag);
5254

55+
// Use proper numeric version comparison
56+
NSString *latestVersion = [self extractVersionString:tag];
57+
NSString *currentVersion = [self extractVersionString:versionString];
58+
NSComparisonResult result = [latestVersion compare:currentVersion options:NSNumericSearch];
59+
5360
//return result based on version strings
54-
completion(([newVesion compare:currentVersion] == NSOrderedDescending),[NSURL URLWithString:[responseObj valueForKey:@"html_url"]]);
61+
completion((result == NSOrderedDescending), [NSURL URLWithString:[responseObj valueForKey:@"html_url"]]);
5562
}else{
5663
completion(false, nil);
5764
}
@@ -66,5 +73,26 @@ + (void)isNewVersionAvailableCompletion:(void (^)(bool available, NSURL *url))co
6673
}
6774
}
6875

76+
+ (NSString *)extractVersionString:(NSString *)input {
77+
if (!input) return @"0";
78+
// Remove everything except digits and dots
79+
NSMutableString *version = [NSMutableString string];
80+
BOOL foundDigit = NO;
81+
for (NSUInteger i = 0; i < input.length; i++) {
82+
unichar c = [input characterAtIndex:i];
83+
if (c >= '0' && c <= '9') {
84+
[version appendFormat:@"%C", c];
85+
foundDigit = YES;
86+
} else if (c == '.' && foundDigit) {
87+
[version appendFormat:@"%C", c];
88+
}
89+
}
90+
// Remove trailing dot if any
91+
if (version.length > 0 && [version characterAtIndex:version.length - 1] == '.') {
92+
[version deleteCharactersInRange:NSMakeRange(version.length - 1, 1)];
93+
}
94+
return version.length > 0 ? [version copy] : @"0";
95+
}
96+
6997

7098
@end

0 commit comments

Comments
 (0)