源码摆出来我就没有上传github
1.声明
//// NSString+Iphone.h// 旺旺好房//
// Created by LessYellowFish on 16/11/15.// Copyright © 2016年 LessYellowFish. All rights reserved.//
#import<Foundation/Foundation.h>
@interface NSString (Iphone)
/** 判断手机类型 */
+ (NSString *)iphoneType;
/** 从字符串提取数字 */
+ (NSString *)extractingNumFormString:(NSString *)string;
/** 判断是否是非空字符串 */
+ (BOOL)isBlankString:(NSString *)string;
/** 判断是否是非0非空字符串 */
+ (BOOL)isZeroBlankString:(NSString *)string;
/**
删除字符串中特定字符
*/
+ (NSString *)stringDeleteString:(NSString *)str;
/**
正则判断手机号位数
@param str 带判断的手机号
@return BOOL
*/
+ (BOOL)isValidPhoneNumber:(NSString*)str;
/**
正则判断是否是手机
@param str 带判断的手机号
@return BOOL
*/
#warning 残缺用下面那个吧
+ (BOOL)wl_isMobileNumberClassification:(NSString *)tel;
/**
正则判断是否手机号(自带提示框)
@param str 带判断的手机号
@return BOOL
*/
+ (BOOL)checkTel:(NSString *)str;
@end
2.实现
#import "NSString+Iphone.h"#import@implementation NSString (Iphone)+ (NSString *)iphoneType { // #import struct utsname systemInfo;
uname(&systemInfo);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
if ([platform isEqualToString:@"iPod1,1"])
return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"])
return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"])
return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"])
return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPod5,1"])
return @"iPod Touch 5G";
if ([platform isEqualToString:@"iPad1,1"])
return @"iPad 1G";
if ([platform isEqualToString:@"iPad2,1"])
return @"iPad 2";
if ([platform isEqualToString:@"iPad2,2"])
return @"iPad 2";
if ([platform isEqualToString:@"iPad2,3"])
return @"iPad 2";
if ([platform isEqualToString:@"iPad2,4"])
return @"iPad 2";
if ([platform isEqualToString:@"iPad2,5"])
return @"iPad Mini 1G";
if ([platform isEqualToString:@"iPad2,6"])
return @"iPad Mini 1G";
if ([platform isEqualToString:@"iPad2,7"])
return @"iPad Mini 1G";
if ([platform isEqualToString:@"iPad3,1"])
return @"iPad 3";
if ([platform isEqualToString:@"iPad3,2"])
return @"iPad 3";
if ([platform isEqualToString:@"iPad3,3"])
return @"iPad 3";
if ([platform isEqualToString:@"iPad3,4"])
return @"iPad 4";
if ([platform isEqualToString:@"iPad3,5"])
return @"iPad 4";
if ([platform isEqualToString:@"iPad3,6"])
return @"iPad 4";
if ([platform isEqualToString:@"iPad4,1"])
return @"iPad Air";
if ([platform isEqualToString:@"iPad4,2"])
return @"iPad Air";
if ([platform isEqualToString:@"iPad4,3"])
return @"iPad Air";
if ([platform isEqualToString:@"iPad4,4"])
return @"iPad Mini 2G";
if ([platform isEqualToString:@"iPad4,5"])
return @"iPad Mini 2G";
if ([platform isEqualToString:@"iPad4,6"])
return @"iPad Mini 2G";
if ([platform isEqualToString:@"i386"])
return @"iPhone Simulator";
if ([platform isEqualToString:@"x86_64"])
return @"iPhone Simulator";
return platform;
}
+ (NSString *)extractingNumFormString:(NSString *)string{
NSString *resualt;
NSCharacterSet* nonDigits =[[NSCharacterSet decimalDigitCharacterSet] invertedSet];
int remainSecond =[[string stringByTrimmingCharactersInSet:nonDigits] intValue];
resualt = [NSString stringWithFormat:@"%d",remainSecond];
return resualt;
}
+ (BOOL)isBlankString:(NSString *)string{
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
/** 判断是否是非0非空字符串 */
+ (BOOL)isZeroBlankString:(NSString *)string{
if ([self isBlankString:string]) {
return YES;
}else if([string isEqualToString:@"0"]){
return YES;
}
return NO;
}
/** 删除字符中特定字符串 by super86 */
+ (NSString *)stringDeleteString:(NSString *)str
{
NSMutableString *str1 = [NSMutableString stringWithString:str];
for (int i = 0; i < str1.length; i++) {
unichar c = [str1 characterAtIndex:i];
NSRange range = NSMakeRange(i, 1);
if (c == ',' || c == ' ') {
//此处可以是任何字符
[str1 deleteCharactersInRange:range];
--i;
}
}
NSString *newstr = [NSString stringWithString:str1];
return newstr;
}
//因为各个运营商开头号码不同,这里只匹配11位是比较好的方法
+ (BOOL)isValidPhoneNumber:(NSString*)str {
NSString *regex = @"^[0-9]{11}";
// return [str isValidateByRegex:regex];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pre evaluateWithObject:str];
}
//或者精确点,但需要根据运营商更新
+ (BOOL)wl_isMobileNumberClassification:(NSString *)tel {
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705
* 联通:130,131,132,152,155,156,185,186,1709
* 电信:133,1349,153,180,189,1700
*/
/**
10 * 中国移动:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705
12
*/
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\\\d|705)\\\\d{7}$";
/**
15 * 中国联通:China Unicom
16 * 130,131,132,152,155,156,185,186,1709
17
*/
NSString * CU = @"^1((3[0-2]|5[256]|8[56])\\\\d|709)\\\\d{7}$";
/**
20 * 中国电信:China Telecom
21 * 133,1349,153,180,189,1700
22
*/
NSString * CT = @"^1((33|53|8[09])\\\\d|349|700)\\\\d{7}$";
/**
25 * 大陆地区固话及小灵通
26 * 区号:010,020,021,022,023,024,025,027,028,029
27 * 号码:七位或八位
28
*/
NSString * PHS = @"^0(10|2[0-5789]|\\\\d{3})\\\\d{7,8}$";
if (([self isValidateByRegex:CM withTel:tel])
|| ([self isValidateByRegex:CU withTel:tel])
|| ([self isValidateByRegex:CT withTel:tel])
|| ([self isValidateByRegex:PHS withTel:tel])) {
return YES;
}else {
return NO;
}
}
+ (BOOL)isValidateByRegex:(NSString *)regex withTel:(NSString *)tel{
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pre evaluateWithObject:tel];
}
#pragma mark 正则判断
+ (BOOL)checkTel:(NSString *)str{
if ([str length] == 0) {
[LessHttp showWithStr:@"请输入正确的手机号"];
return NO;
}
NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:str];
if (!isMatch) {
[LessHttp showWithStr:@"请输入正确的手机号"];
return NO;
}
return YES;
}
@end
调用我就不写了,你们可以试试。