版本更新检查

#pragma mark - 保存应用在AppStore上的版本号到本地
+ (void)saveAppStoreVersionToUserDefaults {
    NSString *storeVersion = [kUserDefaults stringForKey:kAppStoreVersionKey];
    NSString *bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
    // 应用当前的version,应该小于等于store上的version。如果不是,则说明应用升级后,UserDefault中保存的store version未更新,需重新设。
    if(nil == storeVersion || [self version:bundleVersion isBiggerThan:storeVersion]) {
        storeVersion = [HYBNetworkEngine obtainLatestAppVersion]; // 获取最新的版本
        if (storeVersion) {
            [kUserDefaults setObject:storeVersion forKey:kAppStoreVersionKey];
        }
    }
    return;
}

#pragma mark - 是否需要更新应用
+ (BOOL)isAppNeedToUpdate:(BOOL)needNetwork {
    NSString *version = nil;
    if (needNetwork) { // 获取应用在appStore上的版本
        version = [HYBNetworkEngine obtainLatestAppVersion];
        if (version) { // 保存到本地
            [kUserDefaults setObject:version forKey:kAppStoreVersionKey];
        }
    } else { // 直接从本地获取
        version = [kUserDefaults stringForKey:kAppStoreVersionKey];
    }
    
    if (!version) {
        return NO;
    }
    
    NSString *bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    if ([self version:version isBiggerThan:bundleVersion]) {
        return YES;
    }
    return NO;
}

+ (BOOL)version:(NSString *)versionA isBiggerThan:(NSString *)versionB {
    NSArray *a = [versionA componentsSeparatedByString:@"."];
    NSArray *b = [versionB componentsSeparatedByString:@"."];
    
    unsigned aa = [[a objectAtIndex:0] intValue];
    unsigned ab = [a count] > 1 ? [[a objectAtIndex:1] intValue] : 0;
    unsigned cc = [a count] > 2 ? [[a objectAtIndex:2] intValue] : 0;
    
    unsigned ba = [[b objectAtIndex:0] intValue];
    unsigned bb = [b count] > 1 ? [[b objectAtIndex:1] intValue] : 0;
    unsigned bc = [b count] > 2 ? [[b objectAtIndex:2] intValue] : 0;
    
    return ((aa > ba) || (aa == ba && ab > bb) || (aa == ba && ab == bb && cc > bc));
}

#pragma mark - 进入AppStore应用
+ (void)goToAppStore {
#if TARGET_IPHONE_SIMULATOR
    NSLog(@"虚拟机不支持APP Store.打开iTunes不会有效果。");
#else
    NSString *iTunesLink = [NSString stringWithFormat:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%@&mt=8", kAppIDInAppStore, nil];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
#endif
}

上一篇:Pytorch实现GCN、GraphSAGE、GAT


下一篇:SP版本Windows Mobile Crossbow简短体验