获取手机系统版本,以及部分用到的正则表达式

做iOS开发已经四个月了,却从没有写过什么东西。So,为了加深自己的印象,将学到的一些新知识或不牢固的知识,写下来。供我自己用,也可以给跟我一样刚入行的同行提供一些参考。

手机的名称、系统版本号等

获取手机的版本号、名称等。所以将网上的资料整理了一下,放到这里,以供参考。

 NSString *systemVersionStr = [[UIDevice currentDevice] systemVersion];//手机系统版本号
 NSString *userPhoneNameStr = [[UIDevice currentDevice] name];//手机名称

下面两种都可以获取手机的系统,区别是:第一种只能获取系统是iPhone或者iPad;第二种可以精确到具体的版本。

第一种,比较简单
NSString *deviceNameStr = [[UIDevice currentDevice] systemName];//手机系统名称
第二种(需要导入头文件: #import )
- (NSString *)iphoneType {
       struct utsname systemInfo;      
       uname(&systemInfo);
       NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];

       if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
       if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
       if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
       if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
       if ([deviceString isEqualToString:@"iPhone3,3"])    return @"Verizon iPhone 4";
       if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
       if ([deviceString isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
       if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
       if ([deviceString isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
       if ([deviceString isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
       if ([deviceString isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
       if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
       if ([deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
       if ([deviceString isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
       if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
       if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
       if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
       if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
       if ([deviceString isEqualToString:@"iPod5,1"])      return @"iPod Touch 5G";
       if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
       if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
       if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
       if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
       if ([deviceString isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
       if ([deviceString isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
       if ([deviceString isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
       if ([deviceString isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
       if ([deviceString isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
       if ([deviceString isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
       if ([deviceString isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
       if ([deviceString isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
       if ([deviceString isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
       if ([deviceString isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
       if ([deviceString isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
       if ([deviceString isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
       if ([deviceString isEqualToString:@"iPad4,3"])      return @"iPad Air";
       if ([deviceString isEqualToString:@"iPad4,4"])      return @"iPad Mini 2G (WiFi)";
       if ([deviceString isEqualToString:@"iPad4,5"])      return @"iPad Mini 2G (Cellular)";
       if ([deviceString isEqualToString:@"iPad4,6"])      return @"iPad Mini 2G";
       if ([deviceString isEqualToString:@"iPad4,7"])      return @"iPad Mini 3 (WiFi)";
       if ([deviceString isEqualToString:@"iPad4,8"])      return @"iPad Mini 3 (Cellular)";
       if ([deviceString isEqualToString:@"iPad4,9"])      return @"iPad Mini 3 (China)";
       if ([deviceString isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (WiFi)";
       if ([deviceString isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (Cellular)";
       if ([deviceString isEqualToString:@"AppleTV2,1"])  return @"Apple TV 2G";
       if ([deviceString isEqualToString:@"AppleTV3,1"])  return @"Apple TV 3";
       if ([deviceString isEqualToString:@"AppleTV3,2"])  return @"Apple TV 3 (2013)";
       if ([deviceString isEqualToString:@"i386"])        return @"Simulator";
       if ([deviceString isEqualToString:@"x86_64"])      return @"Simulator";
       NSLog(@"NOTE: Unknown device type: %@", deviceString);

       return deviceString;
}

正则表达式

1、验证邮箱
- (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

2、验证手机号码
- (BOOL)validateMobile{
    NSString *MOBILE = @"^1[34578]\\d{9}$";
    NSPredicate *regexTestMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];
    if ([regexTestMobile evaluateWithObject:self]) {
           return YES;
    }else {
            return NO;
}

3、验证车牌号
+ (BOOL) validateCarNo:(NSString *)carNo
{
    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    NSLog(@"carTest is %@",carTest);
    return [carTest evaluateWithObject:carNo];
}

4、车型
+ (BOOL) validateCarType:(NSString *)CarType
{
    NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
    return [carTest evaluateWithObject:CarType];
}

5、用户名
+ (BOOL) validateUserName:(NSString *)name
{
    NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:name];
    return B;
}

6、密码
+ (BOOL) validatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}

7、昵称
+ (BOOL) validateNickname:(NSString *)nickname
{
    NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [passWordPredicate evaluateWithObject:nickname];
}

8、身份证号
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

9、判断是否有特殊符号
- (BOOL)effectivePassword
{
//    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"&\"< >\\/*/null & /* /NULL"];
//    NSString *trimmedString = [self stringByTrimmingCharactersInSet:set];
//    return trimmedString;
     NSString *regex = @"[a-zA-Z0-9]{6,20}";
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
     return [predicate evaluateWithObject:self];
}

你可能感兴趣的:(获取手机系统版本,以及部分用到的正则表达式)