需要导入的头文件有
#include <ifaddrs.h>
#include <arpa/inet.h>
#import <CommonCrypto/CommonDigest.h>
- 做微信支付的时候用到过的
- 获取用户端的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;
}
- 生成一个15位数的随机订单号
+ (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;
}
- 生成任意长度的随机数
+ (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中的特定参数对应的值
+ (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;
}
- 根据时间戳得到现在的年纪
+ (instancetype)stringGetAgeByTime:(long)totoal
{
NSDate *selectDate = [NSDate dateWithTimeIntervalSince1970:totoal];
//选择的日期到现在日期的距离(算年龄)
NSTimeInterval dateDiff = [selectDate timeIntervalSinceNow];
int ageDate = trunc(dateDiff/(60*60*24))/365;
NSString *ageString = [NSString stringWithFormat:@"%d",abs(ageDate)];
return ageString;
}
- 根据出生时候的时间戳算星座
+ (instancetype)getConstellationByTime:(long)totoal
{
NSDate *selectDate = [NSDate dateWithTimeIntervalSince1970:totoal];
//算出选择的月份
NSDateFormatter *monthFormatter = [[NSDateFormatter alloc] init];
[monthFormatter setDateFormat:@"MM"];//设置样式
NSString *month = [monthFormatter stringFromDate:selectDate];//选择日期
//算出选择的哪天
NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init];
[dayFormatter setDateFormat:@"dd"];//设置样式
NSString *day = [dayFormatter stringFromDate:selectDate];
//根据年月日来判断星座
NSString *constellation = @"魔羯座水瓶座双鱼座白羊座金牛座双子座巨蟹座狮子座处女座天秤座天蝎座射手座魔羯座";
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号
- (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;
}
- 判断字符串是不是手机号
- (BOOL)stringIsPhoneNumber
{
//手机号筛选
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