近期项目开发,运用到要获取UUID转MD5,可是iOS7不能使用获取的UDID的接口(涉及到隐私),获取MAC地址的方式的接口在iOS7下也废弃了.眼下可能的就是获取UUID了,可是在iOS7下,UUID一个不好的地方是每次调用一次,生成的就不一样,这个会导致一旦APP卸载重装,UUID就变了.

        以下,介绍一种比較方便的方法,来解决这一问题:

         1.导入Security.framework

         2.在github上http://github.com/soffes/sskeychain下载SSKeychain开源库,引入SSKeychain目录到你的project

         

- (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



@測试后发现,uuid在删除后仍会改变,后面百度了这个博客,大家能够參考:

http://www.cnblogs.com/smileEvday/p/UDID.html

posted on 2017-07-09 21:01  lxjshuju  阅读(238)  评论(0编辑  收藏  举报