需要導(dǎo)入的頭文件有
#include <ifaddrs.h>
#include <arpa/inet.h>
#import <CommonCrypto/CommonDigest.h>
- 做微信支付的時(shí)候用到過(guò)的
- 獲取用戶端的IP地址
+ (instancetype)stringWithIP
{
NSString *address = @"an error occurred when obtaining ip address";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0) { // 0 表示獲取成功
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;
}
}
freeifaddrs(interfaces);
return address;
}
- md5加密
+ (instancetype)stringWithMD5:(NSString *)str
{
const char *cStr = [str UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (unsigned int)strlen(cStr), digest );
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02X", digest[i]];
return output;
}
- sha1加密
+ (instancetype)stringWithsha1:(NSString *)str
{
const char *cstr = [str cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:str.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (unsigned int)data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}
- 生成一個(gè)15位數(shù)的隨機(jī)訂單號(hào)
+ (instancetype)stringWithTradeNO
{
static int kNumber = 15;
NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString *resultStr = [[NSMutableString alloc] init];
srand(time(0));
for (int i = 0; i < kNumber; i++)
{
unsigned index = rand() % [sourceStr length];
NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
[resultStr appendString:oneStr];
}
return resultStr;
}
- 生成任意長(zhǎng)度的隨機(jī)數(shù)
+ (instancetype)stringWithRandomBit:(NSInteger)bit {
char data[bit];
for (NSInteger i = 0; i < bit; i++) {
data[i] = (char)('A' + (arc4random_uniform(26)));
}
return [[NSString alloc] initWithBytes:data length:bit encoding:NSUTF8StringEncoding];
}
- 獲取url中的特定參數(shù)對(duì)應(yīng)的值
+ (instancetype)stringResolutionUrlStr:(NSString *)webaddress WithKey:(NSString *)CS
{
NSError *error;
NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",CS];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:webaddress
options:0
range:NSMakeRange(0, [webaddress length])];
for (NSTextCheckingResult *match in matches) {
NSString *tagValue = [webaddress substringWithRange:[match rangeAtIndex:2]];
return tagValue;
}
return nil;
}
- 判斷字符串中是否有中文
+ (BOOL)stringIsContainChineseWithStr:(NSString *)str
{
for(int i=0; i< [str length];i++){
int a = [str characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff) {
return YES;
}
}
return NO;
}
- 根據(jù)時(shí)間戳得到現(xiàn)在的年紀(jì)
+ (instancetype)stringGetAgeByTime:(long)totoal
{
NSDate *selectDate = [NSDate dateWithTimeIntervalSince1970:totoal];
//選擇的日期到現(xiàn)在日期的距離(算年齡)
NSTimeInterval dateDiff = [selectDate timeIntervalSinceNow];
int ageDate = trunc(dateDiff/(60*60*24))/365;
NSString *ageString = [NSString stringWithFormat:@"%d",abs(ageDate)];
return ageString;
}
- 根據(jù)出生時(shí)候的時(shí)間戳算星座
+ (instancetype)getConstellationByTime:(long)totoal
{
NSDate *selectDate = [NSDate dateWithTimeIntervalSince1970:totoal];
//算出選擇的月份
NSDateFormatter *monthFormatter = [[NSDateFormatter alloc] init];
[monthFormatter setDateFormat:@"MM"];//設(shè)置樣式
NSString *month = [monthFormatter stringFromDate:selectDate];//選擇日期
//算出選擇的哪天
NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init];
[dayFormatter setDateFormat:@"dd"];//設(shè)置樣式
NSString *day = [dayFormatter stringFromDate:selectDate];
//根據(jù)年月日來(lái)判斷星座
NSString *constellation = @"魔羯座水瓶座雙魚(yú)座白羊座金牛座雙子座巨蟹座獅子座處女座天秤座天蝎座射手座魔羯座";
NSString *astroFormat = @"102123444543";
NSString *result = [NSString stringWithFormat:@"%@",[constellation substringWithRange:NSMakeRange([month intValue] * 3 - ([day intValue] < [[astroFormat substringWithRange:NSMakeRange(([month intValue] - 1), 1)] intValue] - ( - 19)) * 3,3)]];
return result;
}
- 判斷字符串是不是qq號(hào)
- (BOOL)stringIsQQNumber
{
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{5,15}$" options:0 error:nil];
if (regex) {
NSTextCheckingResult * resulte = [regex firstMatchInString:self options:0 range:NSMakeRange(0, self.length)];
if (resulte) {
return YES;
}
else
{
return NO;
}
}
return NO;
}
- 判斷字符串是不是手機(jī)號(hào)
- (BOOL)stringIsPhoneNumber
{
//手機(jī)號(hào)篩選
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:@"^[1][358][0-9]{9}" options:0 error:nil];
if (regex) {
NSTextCheckingResult * resulte = [regex firstMatchInString:self options:0 range:NSMakeRange(0,self.length)];
if (resulte) {
return YES;
}
else
{
return NO;
}
}
return NO;
}
- [以上的類別鏈接][1]
[1]:https://github.com/liyang123/NSString-.git