iOS7獲取UUID以及轉換MD5 – iPhone手機開發技術文章 iPhone軟體開發教學課程

最近項目開發,運用到要獲取UUID轉MD5,但是iOS7不能使用獲取的UDID的接口(涉及到隱私),獲取MAC地址的方式的接口在iOS7下也廢棄瞭.目前可能的就是獲取UUID瞭,但是在iOS7下,UUID一個不好的地方是每次調用一次,生成的就不一樣,這個會導致一旦APP卸載重裝,UUID就變瞭.

下面,介紹一種比較方便的方法,來解決這一問題:

1.導入Security.framework

2.在github上https://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 
@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

發佈留言