如何在ios5中使用identifierForVendor。

时间:2012-09-26 10:28:16

标签: iphone ios5 ios6 uidevice

我使用以下代码获取标识符

        deviceName = [[UIDevice currentDevice]uniqueIdentifier];

但我收到警告uniqueIdentifier在ios5中已弃用。

那么如何在ios5中获取标识符值?

4 个答案:

答案 0 :(得分:18)

您可以使用以下代码

if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
    // This will run if it is iOS6 or higher
    return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
} else {
   // This will run before iOS6 and you can use openUDID or other 
   // method to generate an identifier
}

通过这种方式,您可以保持之前的最低要求。

此标识符对于来自一个供应商的所有应用程序是唯一的。如果您需要设备的唯一标识符,则需要使用:

if (!NSClassFromString(@"ASIdentifierManager")) {
    // This will run before iOS6 and you can use openUDID, per example...
    return [OpenUDID value];
}
return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

警告:如果设备已“通过空中”更新,则iOS6上存在报告“空”标识符的错误 - More info

答案 1 :(得分:1)

在ios 7中你最好使用[UIDevice identifierForVendor],否则你会遇到麻烦,因为只返回假的MAC。

答案 2 :(得分:0)

你不能再获得UUID了。禁止这样做,Apple会拒绝您的应用。

答案 3 :(得分:0)

J.Costa 演示的方法是适用于iOS 6的方法。

但是,如果您想在用户升级手机时或在多个应用中使用相同的标识符(除非他们共享相同的捆绑种子ID),请使用:https://github.com/blackpixel/BPXLUUIDHandler

非常有用!