1辐益、郵箱驗(yàn)證:
+ (BOOL)isValidateEmail:(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、密碼驗(yàn)證:
+ (BOOL)isValidatePass:(NSString *)password
{
NSString *passRegex = @"[a-zA-Z0-9]{6,18}";
NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passRegex];;
return [passwordTest evaluateWithObject:password];
}
3脱吱、手機(jī)號(hào)驗(yàn)證:
+ (BOOL) isValidateMobile:(NSString *)mobile
{
//手機(jī)號(hào)以13智政, 15,18開(kāi)頭箱蝠,八個(gè) \d 數(shù)字字符
NSString *phoneRegex = @"^[1][3,4,5,7,8]+\\d{9}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
4续捂、短信驗(yàn)證碼驗(yàn)證:
+ (BOOL)isValidateVCode:(NSString *)vCode
{
NSString *vCodeRegex = @"[0-9]{6}";
NSPredicate *vCodeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",vCodeRegex];;
return [vCodeTest evaluateWithObject:vCode];
}
5.身份證驗(yàn)證:
+ (BOOL) IsIdentityCard:(NSString *)IDCardNumber
{
if (IDCardNumber.length <= 0)?
{
return NO;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate
predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:IDCardNumber];
}
6、銀行卡:
+ (BOOL) IsBankCard:(NSString *)cardNumber
{
if(cardNumber.length==0)
{
return NO;
}
NSString *digitsOnly = @"";
char c;
for (int i = 0; i < cardNumber.length; i++)
{
c = [cardNumber characterAtIndex:i];
if (isdigit(c))
{
digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
}
}
int sum = 0;
int digit = 0;
int addend = 0;
BOOL timesTwo = false;
for (NSInteger i = digitsOnly.length - 1; i >= 0; i--)
{
digit = [digitsOnly characterAtIndex:i] - '0';
if (timesTwo)
{
addend = digit * 2;
if (addend > 9) {
addend -= 9;
}
}
else {
addend = digit;
}
sum += addend;
timesTwo = !timesTwo;
}
int modulus = sum % 10;
return modulus == 0;
}
7宦搬、圖片等比例壓縮:
+(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = defineWidth;
CGFloat targetHeight = height / (width / targetWidth);
CGSize size = CGSizeMake(targetWidth, targetHeight);
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if(CGSizeEqualToSize(imageSize, size) == NO){
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if(widthFactor > heightFactor){
scaleFactor = widthFactor;
}
else{
scaleFactor = heightFactor;
}
scaledHeight = height * scaleFactor;
if(widthFactor > heightFactor){
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}else if(widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(size);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil){
NSLog(@"scale image fail");
}
UIGraphicsEndImageContext();
return newImage;
}
8牙瓢、圖片不等比例壓縮:
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
9、獲取通訊錄(可以實(shí)現(xiàn)打電話):
+(NSMutableDictionary *)readAddr{
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:0];
// Create addressbook data model
ABAddressBookRef addressBook;
if (&ABAddressBookCreateWithOptions != NULL)
{
CFErrorRef error = nil;
addressBook = ABAddressBookCreateWithOptions(NULL, &error);
//等待同意后向下執(zhí)行
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
//等待同意后向下執(zhí)行
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error){
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
NSLog(@"通訊錄已授權(quán)");
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"系統(tǒng)提示" message:@"請(qǐng)?jiān)趇Phone的“設(shè)置-隱私-通訊錄”選項(xiàng)中间校,選擇允許訪問(wèn)你的通訊錄矾克。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return nil;
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
}
//取得本地通信錄名柄
ABAddressBookRef tmpAddressBook = addressBook;
//取得本地所有聯(lián)系人記錄
NSArray* tmpPeoples = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);
for(id tmpPerson in tmpPeoples){
//獲得通訊錄用戶的電話
ABMultiValueRef tmpPhones = ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonPhoneProperty);
//判斷電話數(shù)量是否大于0,沒(méi)有電話的 就不要了
if (ABMultiValueGetCount(tmpPhones) > 0) {
//遍歷用戶電話
for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++){
//取出電話號(hào)碼
NSString *tmpPhoneIndex = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);
//將取出來(lái)的電話號(hào)碼中的特殊字符替換為空字符串
NSString *phone;
phone = [tmpPhoneIndex stringByReplacingOccurrencesOfString:@"-" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""];
phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];
//計(jì)算電話號(hào)碼字符串中最后11位從哪1位開(kāi)始
NSInteger phoneFromIndex = [phone length] - 11;
if (phoneFromIndex >= 0){
NSString *phoneStr = [phone substringFromIndex:phoneFromIndex];
//判斷是否為手機(jī)號(hào)
if ([self isValidateMobile:phoneStr]) {
//獲取的聯(lián)系人單一屬性:First name
NSString *tmpFirstName = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonFirstNameProperty);
//獲取的聯(lián)系人單一屬性:Last name
NSString *tmpLastName =? (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonLastNameProperty);
//拼出用戶的姓名,如果不判斷是否=nil撇簿,顯示出來(lái)就是null
NSString *userName;
if(tmpLastName && tmpFirstName){
userName = [NSString stringWithFormat:@"%@%@",tmpLastName,tmpFirstName];
}else{
if (tmpLastName) {
userName = tmpLastName;
}else if (tmpFirstName){
userName = tmpFirstName;
}else{
userName = @"";
}
}
[dict setValue:userName forKey:phoneStr];
}
}
}
}
CFRelease(tmpPhones);
}
//釋放內(nèi)存
CFRelease(tmpAddressBook);
return dict;
}
10聂渊、存到本地沙河:
+(NSString *)documentFilePath:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *filePathStr = [documentDirectory stringByAppendingPathComponent:fileName];
return filePathStr;
}
11差购、正則匹配用戶密碼6-20位數(shù)字和字母組合:
+ (BOOL)checkPassword:(NSString *) password
{
NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,20}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",pattern];
BOOL isMatch = [pred evaluateWithObject:password];
return isMatch;
}