IOS开发中常用的工具类方法

一、MD5加密

#import 

+ (NSString*)md5Digest:(NSString*)input{
constchar* str = [inputUTF8String];
unsignedcharresult[CC_MD5_DIGEST_LENGTH];
CC_MD5(str,strlen(str), result);
NSMutableString*ret =[NSMutableStringstringWithCapacity:CC_MD5_DIGEST_LENGTH*2];//
for(inti =0; i
[retappendFormat:@"%02x",result[i]];
}
returnret;
}

二、获取当前控制器

+ (UIViewController*)appRootViewController{
UIViewController*appRootVC = [UIApplicationsharedApplication].keyWindow.rootViewController;
UIViewController*topVC = appRootVC;
while(topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
returntopVC;
}

+ (UIViewController*)currentViewController{
return[selfcurrentViewController:nil];
}

+ (UIViewController*)currentViewController:(UIViewController*) baseVc{
UIViewController*nav = [selfappRootViewController];
if(baseVc){
nav = baseVc;
}
if([navisKindOfClass:[UINavigationControllerclass]]) {
UINavigationController*nav2 = (UINavigationController*) nav;
return[nav2.viewControllerslastObject];
}
elseif([navisKindOfClass:[UITabBarControllerclass]]){
UITabBarController*tabvc = (UITabBarController*)nav;
UIViewController* tabbarChildNav = [tabvc.viewControllersobjectAtIndex:tabvc.selectedIndex];
return[selfcurrentViewController:tabbarChildNav];
}
returnnav;
}

三、json字符串转字典NSDictionary

+ (NSDictionary*)dictionaryWithJsonString:(NSString*)jsonString{
if(jsonString ==nil) {
returnnil;
}
NSData*jsonData = [jsonStringdataUsingEncoding:NSUTF8StringEncoding];
NSError*err;
NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err){
NSLog(@"json解析失败:%@",err);
return nil;
}
returndic;
}

四、验证是否是数字

+ (BOOL)isPureInt:(NSString*)string{
NSScanner* scan = [NSScannerscannerWithString:string];
intval;
return[scanscanInt:&val] && [scanisAtEnd];
}

五、字符串判空

+ (BOOL)stringIsEmpty:(NSString*) text
{
if(!text) {
returnYES;
}
if([textisEqual: [NSNullnull]]){
returnYES;
}
if([textisEqualToString:@""]) {
returnYES;
}
if([textisEqualToString:@"nil"]){
returnYES;
}
elseif([textisEqualToString:@""]){
returnYES;
}
returnNO;
}

你可能感兴趣的:(IOS开发中常用的工具类方法)