iOS开发之常用小功能集合

由于一些简单的用法几乎每个项目都会用到,用到的时候都要翻以前的项目代码,觉得很麻烦,所以就想着整理一下,方便使用

获取软件版本号

NSDictionary *infoDic=[[NSBundle mainBundle]infoDictionary];
NSString *version=[infoDic objectForKey:@"CFBundleShortVersionString"];

提示用户是否需要更新

NSString *version1 = @"";
  //该app的链接地址  
NSURL *checkUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=**********"]];

NSString *appInfoString = [NSString stringWithContentsOfURL:checkUrl encoding:NSUTF8StringEncoding error:nil];

NSError *error;

NSData *JSONData = [appInfoString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *appInfo = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:&error];
     if (appInfo) {
        NSArray *resultsAry = appInfo[@"results"];
        NSDictionary *resultsDis = resultsAry.firstObject;
        version1 = resultsDis[@"version"];
     }

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

NSString *thisVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    if ([version1 floatValue] > [thisVersion floatValue]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"发现新版本,是否需要更新?" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"暂时不需要" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/%E5%B8%85%E5%8A%A8%E5%AE%9A%E5%90%91%E8%B6%8A%E9%87%8E/id*********?mt=8&uo=4"]];
            
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }

将字符串转换为时分秒的时间格式

+(NSString *)timeChange:(NSString *)second{
    
    int sec=[second intValue];
    
    int hour=sec/3600;
    int min=(sec-3600*hour)/60;
    int s=sec-3600*hour-60*min;
    NSString *sStr;
    if (s<10) {
        sStr=[NSString stringWithFormat:@"0%i",s];
    }else{
        sStr=[NSString stringWithFormat:@"%i",s];
    }
    NSString *minStr;
    if (min<10) {
        minStr=[NSString stringWithFormat:@"0%i",min];
    }else{
        minStr=[NSString stringWithFormat:@"%i",min];
    }
    NSString *hourStr=[NSString stringWithFormat:@"%i",hour];
    
    return [NSString stringWithFormat:@"%@:%@:%@",hourStr,minStr,sStr];
    
}

计算label的高度


你可能感兴趣的:(iOS开发之常用小功能集合)