当前位置:编程学习 > wap >>

iOS 替代UDID解决方案--UUID

print?#import <Foundation/Foundation.h> 
 
@inte易做图ce UUID : NSObject 
{} 
@property (nonatomic, readonly, retain) NSString *UUIDString; 
 
+ (UUID *)sharedUUID; 
 
@end 

#import <Foundation/Foundation.h>

@inte易做图ce UUID : NSObject
{}
@property (nonatomic, readonly, retain) NSString *UUIDString;

+ (UUID *)sharedUUID;

@end

 

[plain]
#import "UUID.h" 
 
#define UUID_KEY (@"MobileUUID") 
 
static UUID *_sharedUUID = nil; 
 
@implementation UUID 
@synthesize UUIDString = _UUIDString; 
 
+ (UUID *)sharedUUID; 

    @synchronized(self) 
    { 
        if(_sharedUUID == nil) 
        { 
            _sharedUUID = [[UUID alloc] init]; 
        } 
    } 
    return _sharedUUID; 

 
- (id)init 

    if((self = [super init])) 
    { 
    } 
    return self; 

 
#pragma mark - 
#pragma mark UUIDString 
- (NSString *)UUIDString 

    NSString *uuidString = [[NSUserDefaults standardUserDefaults] objectForKey:UUID_KEY]; 
     
    if(uuidString && [uuidString length]) 
    { 
        return uuidString; 
    } 
    else 
    {    
        CFUUIDRef uuidRef = CFUUIDCreate(nil); 
        CFStringRef stringRef = CFUUIDCreateString(nil, uuidRef); 
        uuidString = (NSString *)CFStringCreateCopy(nil, stringRef); 
        CFRelease(uuidRef); 
        CFRelease(stringRef); 
         
        [[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:UUID_KEY]; 
        [[NSUserDefaults standardUserDefaults] synchronize]; 
         
         return [uuidString autorelease]; 
    } 
     
    return nil; 

 
#pragma mark - 
#pragma mark dealloc 
- (void)dealloc 

    [_UUIDString release]; 
    [_sharedUUID release]; 
     
    [super dealloc]; 

@end 

#import "UUID.h"

#define UUID_KEY (@"MobileUUID")

static UUID *_sharedUUID = nil;

@implementation UUID
@synthesize UUIDString = _UUIDString;

+ (UUID *)sharedUUID;
{
    @synchronized(self)
    {
        if(_sharedUUID == nil)
        {
            _sharedUUID = [[UUID alloc] init];
        }
    }
    return _sharedUUID;
}

- (id)init
{
    if((self = [super init]))
    {
    }
    return self;
}

#pragma mark -
#pragma mark UUIDString
- (NSString *)UUIDString
{
    NSString *uuidString = [[NSUserDefaults standardUserDefaults] objectForKey:UUID_KEY];
   
    if(uuidString && [uuidString length])
    {
        return uuidString;
    }
    else
    {  
        CFUUIDRef uuidRef = CFUUIDCreate(nil);
        CFStringRef stringRef = CFUUIDCreateString(nil, uuidRef);
        uuidString = (NSString *)CFStringCreateCopy(nil, stringRef);
        CFRelease(uuidRef);
        CFRelease(stringRef);
       
        [[NSUserDefaults standardUserDefaults] setObject:uuidString forKey:UUID_KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
       
         return [uuidString autorelease];
    }
   
    return nil;
}

#pragma mark -
#pragma mark dealloc
- (void)dealloc
{
    [_UUIDString release];
    [_sharedUUID release];
   
    [super dealloc];
}
@end

 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,