且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式获取iOS设备的IMEI或UDID

更新时间:2023-12-04 16:17:25

Apple不再允许开发人员以编程方式获取UUID。但是,您可以使用解决方法来唯一标识应用中的设备。下面的代码允许您使用 CFUUIDCreate 创建唯一标识符。

Apple no longer allows developers to obtain the UUID programmatically anymore. There are, however, workarounds that you can use to uniquely identify devices in your app. The code below allows you to create a unique identifier using CFUUIDCreate.

Objective-C:

Objective-C:

- (NSString*)GUIDString {
    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
    NSString *guid = (__bridge NSString *)newUniqueIDString;
    CFRelease(newUniqueIDString);
    CFRelease(newUniqueID);
    return([guid lowercaseString]);
}

Swift:

func GUIDString() -> NSString {
    let newUniqueID = CFUUIDCreate(kCFAllocatorDefault)
    let newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
    let guid = newUniqueIDString as! NSString

    return guid.lowercaseString
}

一旦你有了这个唯一标识符,您可以将其存储在 NSUserDefaults ,核心数据,将其发送到Web服务等,以将此唯一ID与运行您的应用的设备相关联。您也可以在 NSString 上将其用作类方法,如图所示此处

Once you have this unique identifier, you can store it in NSUserDefaults, Core Data, send it up to a web service, etc, to associate this unique id with the device running your app. You can also use this as a class method on NSString as seen here.