你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> ios 常用工具類

ios 常用工具類

編輯:關於IOS

[java]

#import "Utility.h"         @implementation Utility      +(NSString *)createMD5:(NSString *)signString   {       const char*cStr =[signString UTF8String];       unsigned char result[16];       CC_MD5(cStr, strlen(cStr), result);       return[NSString stringWithFormat:              @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",              result[0], result[1], result[2], result[3],              result[4], result[5], result[6], result[7],              result[8], result[9], result[10], result[11],              result[12], result[13], result[14], result[15]              ];   }         +(NSString *)createPostURL:(NSMutableDictionary *)params   {       NSString *postString=@"";       for(NSString *key in [params allKeys])       {           NSString *value=[params objectForKey:key];           postString=[postString stringByAppendingFormat:@"%@=%@&",key,value];       }       if([postString length]>1)       {           postString=[postString substringToIndex:[postString length]-1];       }       return postString;   }      +(NSString *)getCurrentDate   {       NSDateFormatter *dateFormatter=[[[NSDateFormatter alloc] init] autorelease];       [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];       return [dateFormatter stringFromDate:[NSDate date]];   }      +(BOOL) connectedToNetwork   {       // Create zero addy        struct sockaddr_in zeroAddress;       bzero(&zeroAddress, sizeof(zeroAddress));       zeroAddress.sin_len = sizeof(zeroAddress);       zeroAddress.sin_family = AF_INET;              // Recover reachability flags        SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);           SCNetworkReachabilityFlags flags;              BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);       CFRelease(defaultRouteReachability);              if (!didRetrieveFlags)        {           printf("Error. Could not recover network reachability flags/n");           return NO;       }              BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);       BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);       return (isReachable && !needsConnection) ? YES : NO;   }      // Direct from Apple. Thank you Apple    + (BOOL)addressFromString:(NSString *)IPAddress address:(struct sockaddr_in *)address   {       if (!IPAddress || ![IPAddress length]) return NO;              memset((char *) address, sizeof(struct sockaddr_in), 0);       address->sin_family = AF_INET;       address->sin_len = sizeof(struct sockaddr_in);              int conversionResult = inet_aton([IPAddress UTF8String], &address->sin_addr);       if (conversionResult == 0) {           NSAssert1(conversionResult != 1, @"Failed to convert the IP address string into a sockaddr_in: %@", IPAddress);           return NO;       }              return YES;   }      + (NSString *) getIPAddressForHost: (NSString *) theHost   {       theHost=[theHost substringFromIndex:7];       //NSLog(@"%@",theHost);        struct hostent *host = gethostbyname([theHost UTF8String]);       if (!host) {herror("resolv"); return NULL; }       struct in_addr **list = (struct in_addr **)host->h_addr_list;       NSString *addressString = [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding];       return addressString;   }         + (BOOL) hostAvailable: (NSString *) theHost   {              NSString *addressString = [self getIPAddressForHost:theHost];       if (!addressString)       {           printf("Error recovering IP address from host name/n");           return NO;       }              struct sockaddr_in address;       BOOL gotAddress = [self addressFromString:addressString address:&address];              if (!gotAddress)       {           printf("Error recovering sockaddr address from %s/n", [addressString UTF8String]);           return NO;       }              SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&address);       SCNetworkReachabilityFlags flags;              BOOL didRetrieveFlags =SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);       CFRelease(defaultRouteReachability);              if (!didRetrieveFlags)       {           printf("Error. Could not recover network reachability flags/n");           return NO;       }              BOOL isReachable = flags & kSCNetworkFlagsReachable;       return isReachable ? YES : NO;;   }         //通過區分字符串       +(BOOL)validateEmail:(NSString*)email   {       if((0 != [email rangeOfString:@"@"].length) &&          (0 != [email rangeOfString:@"."].length))       {           NSCharacterSet* tmpInvalidCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];           NSMutableCharacterSet* tmpInvalidMutableCharSet = [[tmpInvalidCharSet mutableCopy] autorelease];           [tmpInvalidMutableCharSet removeCharactersInString:@"_-"];                                 NSRange range1 = [email rangeOfString:@"@"                                         options:NSCaseInsensitiveSearch];                      //取得用戶名部分            NSString* userNameString = [email substringToIndex:range1.location];           NSArray* userNameArray   = [userNameString componentsSeparatedByString:@"."];                      for(NSString* string in userNameArray)           {               NSRange rangeOfInavlidChars = [string rangeOfCharacterFromSet: tmpInvalidMutableCharSet];               if(rangeOfInavlidChars.length != 0 || [string isEqualToString:@""])                   return NO;           }                      //取得域名部分            NSString *domainString = [email substringFromIndex:range1.location+1];           NSArray *domainArray   = [domainString componentsSeparatedByString:@"."];                      for(NSString *string in domainArray)           {               NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:tmpInvalidMutableCharSet];               if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])                   return NO;           }                      return YES;       }       else {           return NO;       }   }      //利用正則表達式驗證    +(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];   }      +(BOOL)isValidateString:(NSString *)myString   {       NSCharacterSet *nameCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];       NSRange userNameRange = [myString rangeOfCharacterFromSet:nameCharacters];       if (userNameRange.location != NSNotFound) {           //NSLog(@"包含特殊字符");            return FALSE;       }else{           return TRUE;       }      }     #import "Utility.h"     @implementation Utility   +(NSString *)createMD5:(NSString *)signString {     const char*cStr =[signString UTF8String];     unsigned char result[16];     CC_MD5(cStr, strlen(cStr), result);     return[NSString stringWithFormat:            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",            result[0], result[1], result[2], result[3],            result[4], result[5], result[6], result[7],            result[8], result[9], result[10], result[11],            result[12], result[13], result[14], result[15]            ]; }     +(NSString *)createPostURL:(NSMutableDictionary *)params {     NSString *postString=@"";     for(NSString *key in [params allKeys])     {         NSString *value=[params objectForKey:key];         postString=[postString stringByAppendingFormat:@"%@=%@&",key,value];     }     if([postString length]>1)     {         postString=[postString substringToIndex:[postString length]-1];     }     return postString; }   +(NSString *)getCurrentDate {     NSDateFormatter *dateFormatter=[[[NSDateFormatter alloc] init] autorelease];     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];     return [dateFormatter stringFromDate:[NSDate date]]; }   +(BOOL) connectedToNetwork {     // Create zero addy     struct sockaddr_in zeroAddress;     bzero(&zeroAddress, sizeof(zeroAddress));     zeroAddress.sin_len = sizeof(zeroAddress);     zeroAddress.sin_family = AF_INET;          // Recover reachability flags     SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);         SCNetworkReachabilityFlags flags;          BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);     CFRelease(defaultRouteReachability);          if (!didRetrieveFlags)      {         printf("Error. Could not recover network reachability flags/n");         return NO;     }          BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);     BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);     return (isReachable && !needsConnection) ? YES : NO; }   // Direct from Apple. Thank you Apple + (BOOL)addressFromString:(NSString *)IPAddress address:(struct sockaddr_in *)address {     if (!IPAddress || ![IPAddress length]) return NO;       memset((char *) address, sizeof(struct sockaddr_in), 0);     address->sin_family = AF_INET;     address->sin_len = sizeof(struct sockaddr_in);       int conversionResult = inet_aton([IPAddress UTF8String], &address->sin_addr);     if (conversionResult == 0) { NSAssert1(conversionResult != 1, @"Failed to convert the IP address string into a sockaddr_in: %@", IPAddress);         return NO;     }       return YES; }   + (NSString *) getIPAddressForHost: (NSString *) theHost {     theHost=[theHost substringFromIndex:7];     //NSLog(@"%@",theHost); struct hostent *host = gethostbyname([theHost UTF8String]);     if (!host) {herror("resolv"); return NULL; } struct in_addr **list = (struct in_addr **)host->h_addr_list; NSString *addressString = [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding]; return addressString; }     + (BOOL) hostAvailable: (NSString *) theHost {       NSString *addressString = [self getIPAddressForHost:theHost];     if (!addressString)     {         printf("Error recovering IP address from host name/n");         return NO;     }       struct sockaddr_in address;     BOOL gotAddress = [self addressFromString:addressString address:&address];       if (!gotAddress)     { printf("Error recovering sockaddr address from %s/n", [addressString UTF8String]);         return NO;     }   SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&address);     SCNetworkReachabilityFlags flags;   BOOL didRetrieveFlags =SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);     CFRelease(defaultRouteReachability);       if (!didRetrieveFlags)     {         printf("Error. Could not recover network reachability flags/n");         return NO;     }       BOOL isReachable = flags & kSCNetworkFlagsReachable;     return isReachable ? YES : NO;; }     //通過區分字符串   +(BOOL)validateEmail:(NSString*)email {     if((0 != [email rangeOfString:@"@"].length) &&        (0 != [email rangeOfString:@"."].length))     {         NSCharacterSet* tmpInvalidCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];         NSMutableCharacterSet* tmpInvalidMutableCharSet = [[tmpInvalidCharSet mutableCopy] autorelease];         [tmpInvalidMutableCharSet removeCharactersInString:@"_-"];                           NSRange range1 = [email rangeOfString:@"@"                                       options:NSCaseInsensitiveSearch];                  //取得用戶名部分         NSString* userNameString = [email substringToIndex:range1.location];         NSArray* userNameArray   = [userNameString componentsSeparatedByString:@"."];                  for(NSString* string in userNameArray)         {             NSRange rangeOfInavlidChars = [string rangeOfCharacterFromSet: tmpInvalidMutableCharSet];             if(rangeOfInavlidChars.length != 0 || [string isEqualToString:@""])                 return NO;         }                  //取得域名部分         NSString *domainString = [email substringFromIndex:range1.location+1];         NSArray *domainArray   = [domainString componentsSeparatedByString:@"."];                  for(NSString *string in domainArray)         {             NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:tmpInvalidMutableCharSet];             if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])                 return NO;         }                  return YES;     }     else {         return NO;     } }   //利用正則表達式驗證 +(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]; }   +(BOOL)isValidateString:(NSString *)myString {     NSCharacterSet *nameCharacters = [[NSCharacterSet characterSetWithCharactersInString:@"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"] invertedSet];www.2cto.com     NSRange userNameRange = [myString rangeOfCharacterFromSet:nameCharacters];     if (userNameRange.location != NSNotFound) {         //NSLog(@"包含特殊字符");         return FALSE;     }else{         return TRUE;     }   }    
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved