最近项目开发,运用到要获取UUID转MD5,但是iOS7不能使用获取的UDID的接口(涉及到隐私),获取MAC地址的方式的接口在iOS7下也废弃了.目前可能的就是获取UUID了,但是在iOS7下,UUID一个不好的地方是每次调用一次,生成的就不一样,这个会导致一旦APP卸载重装,UUID就变了.
下面,介绍一种比较方便的方法,来解决这一问题:
1.导入Security.framework
2.在github上http://github.com/soffes/sskeychain下载SSKeychain开源库,引入SSKeychain文件夹到你的工程
- (NSString *)getIOSUUID { NSString *retrieveuuid = [SSKeychain passwordForService:@"com.mohe.userinfo"account:@"uuid"]; if ( retrieveuuid == nil || [retrieveuuid isEqualToString:@""]){ CFUUIDRef uuid = CFUUIDCreate(NULL); assert(uuid != NULL); CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid); retrieveuuid = [NSString stringWithFormat:@"%@", uuidStr]; [SSKeychain setPassword:retrieveuuid forService:@"com.mohe.userinfo"account:@"uuid"]; } return retrieveuuid; }
@转换MD5格式的话可以写个类目:
#import "NSString+MD5.h" #import <CommonCrypto/CommonDigest.h> @implementation NSString (MD5) - (NSString *)md5HexDigest { const char *original_str = [self UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(original_str, (int)strlen(original_str), result); NSMutableString *hash = [NSMutableString string]; for (int i = 0; i < 16; i++) [hash appendFormat:@"%02X", result[i]]; return [hash lowercaseString]; } @end