各種小方法的歸納

1>>驗證手機號的正則表達式

-(BOOL) isValidateMobile:(NSString *)mobile

{

//手機號以13, 15,18開頭,八個 \d 數字字符

NSString *phoneRegex = @"^(1[3|4|5|7|8])\\d{9}$";

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:mobile];

}

2>>驗證郵箱的正則表達式

+ (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];

}

3>>驗證身份證的正則

+ (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];

}

4>>毛玻璃效果

- (UIVisualEffectView *)effectViewWithFrame:(CGRect)frame

{

UIBlurEffect * effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

UIVisualEffectView * effectView = [[UIVisualEffectView alloc]initWithEffect:effect];

effectView.frame = frame;

return effectView;

}

5>>判斷字符串中是否含有特定字符串

- (BOOL)isHaveString:(NSString *)string1 InString:(NSString *)string2

{

NSRange _range = [string2 rangeOfString:string1];

if (_range.location != NSNotFound) {

return YES;

}

return NO;

}

6>>判斷字符串中是否含有中文

- (BOOL)isHaveChineseInString:(NSString *)string

{

for (NSInteger i = 0; i < [string length]; i++) {

int a = [string characterAtIndex:i];

if (a > 0x4e00 && a < 0x9fff) {

return YES;

}

}

return NO;

}

7>>判斷字符串是否全是數字

- (BOOL)isAllNum:(NSString *)string

{

unichar c;

for (int i = 0; i < [string length]; i ++) {

c = [string characterAtIndex:i];

if (!isdigit(c)) {

return NO;

}

}

return YES;

}

8>>截屏

- (void) shootScreen

{

UIGraphicsBeginImageContext(remoteVideoSurface.bounds.size);

[remoteVideoSurface.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(image,self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

[SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"success",nil)];

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error

contextInfo:(void *)contextInfo

{

if (error != NULL)

{

NSLog(@"截圖err----%@",error);

}

else? //截圖保存成功

{

}

}

錄像保存到本地// 保存到相冊

NSString *sourcePath = lpFileName;

UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);

9>>獲取ip地址

+ (NSString *)getIpAddress{

NSString *address = @"error";

struct ifaddrs *interfaces = NULL;

struct ifaddrs *temp_addr = NULL;

int success = 0;

// retrieve the current interfaces - returns 0 on success

success = getifaddrs(&interfaces);

if (success == 0)

{

// Loop through linked list of interfaces

temp_addr = interfaces;

while(temp_addr != NULL)

{

if(temp_addr->ifa_addr->sa_family == AF_INET)

{

// Check if interface is en0 which is the wifi connection on the iPhone

if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])

{

// Get NSString from C String

address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];

}

}

temp_addr = temp_addr->ifa_next;

}

}

// Free memory

freeifaddrs(interfaces);

freeifaddrs(temp_addr);

return address;

}

10>>判斷文件大小

/**

*? MARK:判斷文件大小

*? @param filePath 文件路徑

*/

+ (long long) fileSizeAtPath:(NSString*) filePath

{

NSFileManager* manager = [NSFileManager defaultManager];

if ([manager fileExistsAtPath:filePath]){

return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];

}

return 0;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容