UIDevice的uniqueIdentifier已经被弃用 - 现在该怎么做?

511
刚刚得知,在iOS 5中已将UIDevice唯一标识符属性(uniqueIdentifier property)作废,在iOS 7及以上版本中也不再提供该属性的支持。目前似乎没有可用或即将推出的替代方法或属性。

我们许多现有的应用程序都高度依赖此属性来唯一标识特定设备。那么我们该如何解决这个问题呢?

2011-2012年的文档建议:

特别注意事项

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

但是如果用户卸载并重新安装该应用程序,该值将不同。


1
对于仍在使用uniqueIdentifier的应用程序,iOS7现在返回FFFFFFFF + identifierForVendor,这破坏了许多编写不良的非续订订阅应用程序。 - Rhythmic Fistman
如果你的应用程序幸运地使用了推送通知,你可以使用从苹果推送服务返回的令牌,它对于每个设备都是唯一的。 - Calin Chitu
如果用户不接受推送通知,您是否仍会为该用户获取推送ID? - Chase Roberts
32个回答

7
我建议您将uniqueIdentifier更改为this open source library(真的只有2个简单的类别),它利用设备的MAC地址和应用程序包标识符来生成应用程序中可用作UDID替代的唯一ID。
请记住,与UDID不同,此号码对于每个应用程序都将是不同的。
您只需导入包含的NSStringUIDevice类别,并像这样调用[[UIDevice currentDevice] uniqueDeviceIdentifier]:
#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"
NSString *iosFiveUDID = [[UIDevice currentDevice] uniqueDeviceIdentifier]

你可以在 Github 上找到它,链接如下:

UIDevice with UniqueIdentifier for iOS 5


这里是类别(只有 .m 文件 - 查看 Github 项目获取头文件):

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:

#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

3
从iOS 7开始,苹果将返回MAC地址的常量值。这很有道理,因为MAC地址是敏感信息。 - Roberto

5

4

使用上述提到的SSKeychain和代码。这里是可以复制/粘贴的代码(添加SSKeychain模块):

+(NSString *) getUUID {

//Use the bundle name as the App identifier. No need to get the localized version.

NSString *Appname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];    

//Check if we have UUID already

NSString *retrieveuuid = [SSKeychain passwordForService:Appname account:@"user"];

if (retrieveuuid == NULL)
{

    //Create new key for this app/device

    CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);

    retrieveuuid = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);

    CFRelease(newUniqueId);

    //Save key to Keychain
    [SSKeychain setPassword:retrieveuuid forService:Appname account:@"user"];
}

return retrieveuuid;

}


4

UIDevice identifierForVendor 在iOS 6中引入,可满足您的需求。

identifierForVendor 是一个包含字母和数字的字符串,可唯一地标识设备对应的应用程序供应商。(只读)

@property(nonatomic, readonly, retain) NSUUID *identifierForVendor

这个属性的值对于来自同一供应商并在同一设备上运行的应用程序是相同的。对于来自不同供应商的同一设备上的应用程序以及来自不同设备的应用程序,返回不同的值。
在iOS 6.0及更高版本中可用,并在UIDevice.h中声明。
有关iOS 5的信息,请参阅此链接UIDevice-with-UniqueIdentifier-for-iOS-5

4

MAC地址可以被欺骗,这使得将内容与特定用户绑定或实现黑名单等安全功能的方法变得无用。

经过进一步研究,我认为目前我们没有适当的替代方案。我真诚地希望苹果公司能重新考虑他们的决定。

也许给苹果发电子邮件,或在此问题上提交错误/功能请求会是一个好主意,因为他们可能甚至没有意识到开发人员面临的全部后果。


13
一个有效的观点。然而,我认为即使在越狱手机上,UUID 也可以被伪造/篡改,因此从技术上讲,现有的 [UIDevice uniqueIdentifier] 也同样存在缺陷。 - Oliver Pearmain
3
你可以在服务器上创建一个标识符,然后将其保存在设备上。这是大多数应用程序的做法。我不明白为什么iOS程序员需要特别的东西。 - Sulthan
1
@ Sulthan在iOS上无法工作,因为如果您卸载一个应用程序,则所有数据都将消失,因此没有办法保证唯一设备标识符。 - lkraider
4
如果您将其保存到钥匙串中,则不会出现此问题。无论如何,我从未见过出现这种问题的应用程序。如果应用程序和数据已被删除,则您不需要相同的设备标识符。如果您想要识别用户,请要求他提供电子邮件地址。 - Sulthan
苹果在最新的iOS版本中也禁止了MAC地址访问。 - Ans

3

这是我用来获取iOS 5和iOS 6、7的ID的代码:

- (NSString *) advertisingIdentifier
{
    if (!NSClassFromString(@"ASIdentifierManager")) {
        SEL selector = NSSelectorFromString(@"uniqueIdentifier");
        if ([[UIDevice currentDevice] respondsToSelector:selector]) {
            return [[UIDevice currentDevice] performSelector:selector];
        }
    }
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}

关于编译器警告 PerformSelector may cause a leak because its selector is unknown,你该怎么处理? - Basil Bourque
你不能再使用advertisingIdentifier来实现这个目的,因为苹果公司将会拒绝它。更多信息请参考:http://techcrunch.com/2014/02/03/apples-latest-crackdown-apps-pulling-the-advertising-identifier-but-not-showing-ads-are-being-rejected-from-app-store/ - codeplasma

3
以下代码可帮助获取UDID:
        udid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        NSLog(@"UDID : %@", udid);

2

2

iOS 11引入了DeviceCheck框架,它提供了一种完美的解决方案来唯一标识设备。


1
一种获取UDID的有效方法:
  1. 在应用程序中启动一个Web服务器,其中包含两个页面:一个页面应返回特制的MobileConfiguration配置文件,另一个页面应收集UDID。更多信息请参见这里, 这里这里
  2. 您可以从应用程序内部使用Mobile Safari打开第一个页面,它会将您重定向到Settings.app,并要求安装配置文件。安装配置文件后,UDID将被发送到第二个网页,您可以从应用程序内部访问它。(Settings.app具有所有必要的权限和不同的沙盒规则)。
一个使用RoutingHTTPServer的示例:
import UIKit
import RoutingHTTPServer

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var bgTask = UIBackgroundTaskInvalid
    let server = HTTPServer()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        application.openURL(NSURL(string: "http://localhost:55555")!)
        return true
    }

    func applicationDidEnterBackground(application: UIApplication) {
        bgTask = application.beginBackgroundTaskWithExpirationHandler() {
            dispatch_async(dispatch_get_main_queue()) {[unowned self] in
                application.endBackgroundTask(self.bgTask)
                self.bgTask = UIBackgroundTaskInvalid
            }
        }
    }
}

class HTTPServer: RoutingHTTPServer {
    override init() {
        super.init()
        setPort(55555)
        handleMethod("GET", withPath: "/") {
            $1.setHeader("Content-Type", value: "application/x-apple-aspen-config")
            $1.respondWithData(NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("udid", ofType: "mobileconfig")!)!)
        }
        handleMethod("POST", withPath: "/") {
            let raw = NSString(data:$0.body(), encoding:NSISOLatin1StringEncoding) as! String
            let plistString = raw.substringWithRange(Range(start: raw.rangeOfString("<?xml")!.startIndex,end: raw.rangeOfString("</plist>")!.endIndex))
            let plist = NSPropertyListSerialization.propertyListWithData(plistString.dataUsingEncoding(NSISOLatin1StringEncoding)!, options: .allZeros, format: nil, error: nil) as! [String:String]

            let udid = plist["UDID"]! 
            println(udid) // Here is your UDID!

            $1.statusCode = 200
            $1.respondWithString("see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/iPhoneOTAConfiguration/ConfigurationProfileExamples/ConfigurationProfileExamples.html")
        }
        start(nil)
    }
}

这是 udid.mobileconfig 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>PayloadContent</key>
        <dict>
            <key>URL</key>
            <string>http://localhost:55555</string>
            <key>DeviceAttributes</key>
            <array>
                <string>IMEI</string>
                <string>UDID</string>
                <string>PRODUCT</string>
                <string>VERSION</string>
                <string>SERIAL</string>
            </array>
        </dict>
        <key>PayloadOrganization</key>
        <string>udid</string>
        <key>PayloadDisplayName</key>
        <string>Get Your UDID</string>
        <key>PayloadVersion</key>
        <integer>1</integer>
        <key>PayloadUUID</key>
        <string>9CF421B3-9853-9999-BC8A-982CBD3C907C</string>
        <key>PayloadIdentifier</key>
        <string>udid</string>
        <key>PayloadDescription</key>
        <string>Install this temporary profile to find and display your current device's UDID. It is automatically removed from device right after you get your UDID.</string>
        <key>PayloadType</key>
        <string>Profile Service</string>
    </dict>
</plist>

安装描述文件将失败(我没有实现预期的响应,请参阅文档),但应用程序将获得正确的 UDID。您还应该 签署 mobileconfig 文件

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接