Iphone 会话上的UUID静态Id?

Iphone 会话上的UUID静态Id?,iphone,ios,Iphone,Ios,我想使用UDID的替代方案,并发现: + (NSString *)GetUUID { CFUUIDRef theUUID = CFUUIDCreate(NULL); CFStringRef string = CFUUIDCreateString(NULL, theUUID); CFRelease(theUUID); return [(NSString *)string autorelease]; } 但是在模拟器中,这种方法每次都会给我不同的结果 这只在模拟器里吗 我需要确保在

我想使用UDID的替代方案,并发现:

+ (NSString *)GetUUID
{
  CFUUIDRef theUUID = CFUUIDCreate(NULL);
  CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  CFRelease(theUUID);
  return [(NSString *)string autorelease];
}
但是在模拟器中,这种方法每次都会给我不同的结果

这只在模拟器里吗

我需要确保在实际设备上,该方法始终返回相同的字符串 识别用户

这是真的还是假的


Mirza

CFUUID根本不会持久化

每次调用CFUUIDCreate时,系统都会返回一个全新的唯一标识符


如果要保留此标识符,您需要自己使用NSUserDefaults、Keychain、Pasteboard或其他方法来执行此操作。

CFUUIDCreate在每次调用该函数时都会为您提供一个通用的唯一标识符,因此每次都会得到不同的结果(根据定义)


您可以做的是在会话之间使用NSUserDefaults(例如,NSUserDefaults)来保持这一点,以唯一地标识特定用户(或一组用户的设置)。

一次阅读一行代码,并尝试理解它的功能。
CFUUIDCreate
函数每次调用时都会创建一个新的UUID。这可以解释你的发现。首次启动应用程序时,您需要将该值保存在
NSUserDefaults
*中,并在下次启动应用程序时使用该值:

+ (NSString *)GetUUID
{
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey: @"UUID"];
    if (!string)
    {
        CFUUIDRef theUUID = CFUUIDCreate(NULL);
        string = (NSString*)[CFUUIDCreateString(NULL, theUUID) autorelease];
        CFRelease(theUUID);
        [[NSUserDefaults standardUserDefaults] setObject: string forKey: @"UUID"];
    }
    return string;
}

*使用
NSUserDefaults
有一个小警告-如果用户再次卸载并重新安装应用程序,将再次创建UUID。如果你不能接受这个,那就把它保存在钥匙链里吧。或者,您可能想看看。

它总是不同的。UUID包含时间戳,因此每次调用此函数时,都会得到一个不同的(随机)时间戳。 我在
IDManager
课程中采用了这种方法, 这是来自不同解决方案的集合。KeyChainUtil是从keychain读取的包装器。类似的keychain util可以在中找到

//IDManager.m
/*
替换已弃用的uniqueIdentifier API。苹果公司从2013年5月1日起限制使用该软件。
我们必须考虑,
*iOS 6
-检查钥匙链中是否已存储UUID。那就用这个。
-在这种情况下,这个UUID在整个设备生命周期中是恒定的。钥匙链项目不会随应用程序删除而删除。
*/
#导入“IDManager.h”
#导入“KeychainUtils.h”
#导入“CommonUtil.h”
#如果允许的IPHONE操作系统版本最大值>=60000
#进口
#恩迪夫
#包括
#包括
#包括
#包括
/*苹果公司在回应一项技术支持事件请求时确认了他们系统中的这一缺陷。他们说,供应商和广告商的identifierForVendor和advertisingIdentifier有时返回全零,这在开发版本和从应用商店通过空中下载的应用程序中都可以看到。他们没有解决办法,也不能说什么时候能解决问题*/
#定义KBUGGYASID@“00000000-0000-0000-0000-000000000000”
#布拉格标记
#布拉格标记
@实施管理器
+(NSString*)getUniqueID{
#如果允许的IPHONE操作系统版本最大值>=60000
if(NSClassFromString(@“ASIdentifierManager”)){
NSString*asiID=[[asiIdentifierManager共享管理器]广告标识器]uuiString];
if([ASID比较:KBUGGYASID]==NSOrderedSame){
NSLog(@“错误:此设备返回错误的广告标识符。”);
返回[IDManager getUniqueUUID];
}否则{
返回asiID;
}
}否则{
#恩迪夫
返回[IDManager getUniqueUUID];
#如果允许的IPHONE操作系统版本最大值>=60000
}
#恩迪夫
}
+(NSString*)getUniqueUUID
{
n错误*错误;
NSString*uuid=[KeychainUtils getPasswordForUsername:@“UserName”和ServiceName:@“YourServiceName”错误:&错误];
如果(错误){
NSLog(@“获取此设备的唯一UUID时出错!%@,[Error localizedDescription]);
返回零;
}
如果(!uuid){
DLog(@“未找到UUID。正在创建新的UUID”);
uuid=[IDManager getUUID];
uuid=[CommonUtil md5String:uuid];//出于安全原因创建md5哈希
[KeychainUtils storeUsername:@“UserName”和密码:uuid for ServiceName:@“YourServiceName”更新存在:是错误:&错误];
如果(错误){
NSLog(@“获取此设备的唯一UUID时出错!%@,[Error localizedDescription]);
返回零;
}
}
返回uuid;
}
+(NSString*)ReadUUIFromKeyChain{
n错误*错误;
NSString*uuid=[KeychainUtils getPasswordForUsername:@“UserName”和ServiceName:@“YourServiceName”错误:&错误];
如果(错误){
NSLog(@“获取此设备的唯一UUID时出错!%@,[Error localizedDescription]);
返回零;
}
返回uuid;
}
/*NSUID在iOS 6之后。因此,我们使用CFUUID与iOS 4.3兼容*/
+(NSString*)getUUID
{
CFUUIDRef theUUID=CFUUIDCreate(NULL);
CFStringRef string=CFUUIDCreateString(NULL,theUUID);
cfd释放(theUUID);
返回[(NSString*)字符串自动释放];
}
#pragma标记-MAC地址
/*这在iOS 7中不起作用。它将始终返回一个恒定的MAC地址。
见-https://developer.apple.com/news/?id=8222013a
*/
//返回本地MAC地址
//由FreeBSD黑客电子邮件列表提供
//唯一标识符的最后回退
+(NSString*)getMACAddress
{
int-mib[6];
尺寸透镜;
char*buf;
无符号字符*ptr;
结构if_msghdr*ifm;
结构sockaddr_dl*sdl;
mib[0]=CTL_NET;
mib[1]=AF_路由;
mib[2]=0;
mib[3]=AF_链路;
mib[4]=净注册列表;
if((mib[5]=if_nametoindex(“en0”))==0){
printf(“错误:if_nametoindex Error\n”);
返回NULL;
}
if(sysctl(mib,6,NULL,&len,NULL,0)<0){
printf(“错误:sysctl,取1\n”);
返回NULL;
}
如果((buf=malloc(len))==NULL){
printf(“错误:内存分配错误\n”);
返回NULL;
}
if(sysctl(mib,6,buf,&len,NULL,0)<0){
printf(“错误:sysctl,取2\n”);
免费(buf);
//  IDManager.m


/*
 A replacement for deprecated uniqueIdentifier API. Apple restrict using this from 1st May, 2013.

 We have to consider,
    * iOS <6 have not the ASIIdentifer API
    * When the user upgrade from iOS < 6 to >6
        - Check if there is a UUID already stored in keychain. Then use that. 
            - In that case, this UUID is constant for whole device lifetime. Keychain item is not deleted with application deletion.
 */

#import "IDManager.h"
#import "KeychainUtils.h"
#import "CommonUtil.h"

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000
    #import <AdSupport/AdSupport.h>
#endif

#include <sys/socket.h> 
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>



/*  Apple confirmed this bug in their system in response to a Technical Support Incident request. They said that identifierForVendor and advertisingIdentifier sometimes returning all zeros can be seen both in development builds and apps downloaded over the air from the App Store. They have no work around and can't say when the problem will be fixed. */
#define kBuggyASIID             @"00000000-0000-0000-0000-000000000000"


#pragma mark 


#pragma mark 

@implementation IDManager

+ (NSString *) getUniqueID {

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000

    if (NSClassFromString(@"ASIdentifierManager")) {
        NSString * asiID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
        if ([asiID compare:kBuggyASIID] == NSOrderedSame) {
            NSLog(@"Error: This device return buggy advertisingIdentifier.");
            return [IDManager getUniqueUUID];
        } else {
            return asiID;
        }

    } else {

#endif

        return [IDManager getUniqueUUID];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000
    }
#endif

}

+ (NSString *) getUniqueUUID 
{
    NSError * error;
    NSString * uuid = [KeychainUtils getPasswordForUsername:@"UserName" andServiceName:@"YourServiceName" error:&error];
    if (error) {
        NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
        return nil;
    }
    if (!uuid) {
        DLog(@"No UUID found. Creating a new one.");
        uuid = [IDManager getUUID];
        uuid = [CommonUtil md5String:uuid]; // create md5 hash for security reason
        [KeychainUtils storeUsername:@"UserName" andPassword:uuid forServiceName:@"YourServiceName" updateExisting:YES error:&error];
        if (error) {
            NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
            return nil;
        }
    }
    return uuid;
}

+ (NSString *) readUUIDFromKeyChain {
    NSError * error;
    NSString * uuid = [KeychainUtils getPasswordForUsername:@"UserName" andServiceName:@"YourServiceName" error:&error];
    if (error) {
        NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
        return nil;
    }
    return uuid;
}

/* NSUUID is after iOS 6. So we are using CFUUID for compatibility with iOS 4.3 */
+ (NSString *)getUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

#pragma mark - MAC address

/* THIS WILL NOT WORK IN iOS 7. IT WILL RETURN A CONSTANT MAC ADDRESS ALL THE TIME.
 SEE - https://developer.apple.com/news/?id=8222013a
 */

// Return the local MAC address
// Courtesy of FreeBSD hackers email list
// Last fallback for unique identifier
+ (NSString *) getMACAddress
{
    int                 mib[6];
    size_t              len;
    char                *buf;
    unsigned char       *ptr;
    struct if_msghdr    *ifm;
    struct sockaddr_dl  *sdl;

    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;

    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error\n");
        return NULL;
    }

    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1\n");
        return NULL;
    }

    if ((buf = malloc(len)) == NULL) {
        printf("Error: Memory allocation error\n");
        return NULL;
    }

    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2\n");
        free(buf); // Thanks, Remy "Psy" Demerest
        return NULL;
    }

    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
                           *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];

    free(buf);
    return outstring;
}

+ (NSString *) getHashedMACAddress
{
    NSString * mac = [IDManager getMACAddress];
    return [CommonUtil md5String:mac];
}

@end
NSString *udidVendor = [[[UIDevice currentDevice] identifierForVendor] UUIDString];