Xcode:“不推荐使用唯一标识符”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8818949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode: "uniqueIdentifier deprecated"
提问by FBryant87
I have frequently been told to ignore this compiler warning as it won't be an issue until the next major version of iOS is released. Well... it would be nice if my iPhone app didn't just suddenly stop working one day!
我经常被告知忽略这个编译器警告,因为在 iOS 的下一个主要版本发布之前它不会成为问题。嗯...如果我的 iPhone 应用程序没有突然停止工作,那就太好了!
Weird thing is I didn't have this issue using cocos2d for OpenGL 2.0, it was only the OpenGL 1.1 version which had it.
奇怪的是,我在 OpenGL 2.0 中使用 cocos2d 时没有遇到这个问题,只有 OpenGL 1.1 版本才有这个问题。
Does this mean an alternative was provided in the newer version?
这是否意味着在较新版本中提供了替代方案?
回答by u596219
The "Special Considerations" section in the documentationtells you what the recommended way of obtaining a unique identifier is now:
文档中的“特殊注意事项”部分告诉您现在推荐的获取唯一标识符的方法是什么:
Do not use the
uniqueIdentifier
property. To create a unique identifier specific to your app, you can call theCFUUIDCreate
function to create a UUID, and write it to the defaults database using theNSUserDefaults
class.
请勿使用该
uniqueIdentifier
物业。要创建特定于您的应用程序的唯一标识符,您可以调用该CFUUIDCreate
函数来创建 UUID,然后使用NSUserDefaults
该类将其写入默认数据库。
回答by chown
I would suggest changing over from uniqueIdentifier
to this open source library(2 simple categories really). It utilizes the device's MAC Address along with the App Bundle Identifier to generate a unique ID in your applications that can be used as a UDID replacement.
我建议从这个开源库(实际上是 2 个简单的类别)切换uniqueIdentifier
到这个开源库。它利用设备的 MAC 地址和 App Bundle Identifier 在您的应用程序中生成一个唯一 ID,可用作 UDID 替换。
Keep in mind that unlike the UDID this number will be different for every app.
请记住,与 UDID 不同的是,这个数字对于每个应用程序都是不同的。
You simply need to import the included NSString
and UIDevice
categories and call:
您只需要导入包含的NSString
和UIDevice
类别并调用:
#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"
NSString *iosFiveUDID = [[UIDevice currentDevice] uniqueDeviceIdentifier]
In order to grab the generated device identifier.
为了抓取生成的设备标识符。
You can find it on Github here:
你可以在 Github 上找到它:
https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5
https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5
Heres the code (just the .m files - check the github project for the headers):
下面是代码(只是 .m 文件 - 检查头文件的 github 项目):
UIDevice+IdentifierAddition.m
UIDevice+IdentifierAddition.m
#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:
NSString+MD5Addition.m:
#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