你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 替代UDID解決方案--UUID

iOS 替代UDID解決方案--UUID

編輯:IOS開發綜合

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

#import <Foundation/Foundation.h>

@interface 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

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved