首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode:"uniqueIdentifier弃用“

Xcode:"uniqueIdentifier弃用“
EN

Stack Overflow用户
提问于 2012-01-11 12:07:58
回答 2查看 5.5K关注 0票数 0

我经常被告知忽略这个编译器警告,因为它不会成为一个问题,直到下一个主要版本的iOS发布。好吧..。如果有一天我的iPhone应用没有突然停止工作,那就太好了!

奇怪的是,我在使用cocos2d for OpenGL 2.0时没有遇到这个问题,只有OpenGL 1.1版本才有这个问题。

这是否意味着在新版本中提供了一种替代方案?

EN

回答 2

Stack Overflow用户

发布于 2012-01-11 13:28:29

文档中的“特殊注意事项”部分告诉您,现在获得唯一标识符的推荐方法是:

不要使用uniqueIdentifier属性。要创建特定于应用程序的唯一标识符,可以调用CFUUIDCreate函数来创建UUID,并使用NSUserDefaults类将其写入默认数据库。

票数 5
EN

Stack Overflow用户

发布于 2012-01-11 13:43:11

我建议将uniqueIdentifier改为这个开源库 (实际上是两个简单的类别)。它利用设备的MAC地址和App标识符在应用程序中生成一个唯一的ID,该ID可以用作UDID替换。

请记住,与UDID不同,每个应用程序都会有不同的数字。

只需导入包含的NSStringUIDevice类别并调用:

代码语言:javascript
复制
#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"
NSString *iosFiveUDID = [[UIDevice currentDevice] uniqueDeviceIdentifier]

以获取生成的设备标识符。

你可以在这里找到它:

https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5

下面是代码(只有.m文件-检查github项目的标题):

UIDevice+IdentifierAddition.m

代码语言:javascript
复制
#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"

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

@interface UIDevice(Private)

- (NSString *) macaddress;

@end

@implementation UIDevice (IdentifierAddition)

////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Private Methods

// Return the local MAC addy
// Courtesy of FreeBSD hackers email list
// Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
- (NSString *) macaddress{
    
    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("Could not allocate memory. error!\n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        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;
}

////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public Methods

- (NSString *) uniqueDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];  
    NSString *stringToHash = [NSString stringWithFormat:@"%@%@",macaddress,bundleIdentifier];
    NSString *uniqueIdentifier = [stringToHash stringFromMD5];  
    return uniqueIdentifier;
}

- (NSString *) uniqueGlobalDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *uniqueIdentifier = [macaddress stringFromMD5];    
    return uniqueIdentifier;
}

@end

NSString+MD5Addition.m:

代码语言:javascript
复制
#import "NSString+MD5Addition.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString(MD5Addition)

- (NSString *) stringFromMD5{
    
    if(self == nil || [self length] == 0)
        return nil;
    
    const char *value = [self UTF8String];
    
    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(value, strlen(value), outputBuffer);
    
    NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
        [outputString appendFormat:@"%02x",outputBuffer[count]];
    }
    return [outputString autorelease];
}

@end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8818949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档