【问题标题】:how to get device udid in iphone sdk?如何在 iphone sdk 中获取设备 udid?
【发布时间】:2013-05-25 09:09:24
【问题描述】:

我正在开发 iPhone 应用程序。在这个应用程序中,我需要获取设备 udid。在模拟器中工作正常,但在 iphone 设备中没有得到正确的值,值得到 0。

代码如下;

UIDevice *device = [UIDevice currentDevice];
    NSString *uniqueIdentifier = [device uniqueIdentifier];

谢谢,

【问题讨论】:

  • 你为什么使用 UDID ?现在已弃用。苹果已经限制使用它。
  • 好的,如何获取 iPhone 唯一标识符?你能帮帮我吗?
  • 如何从 iPhone 设备获取唯一 ID?

标签: iphone device udid


【解决方案1】:

我们知道 uniqueIdentifier 在 iOS 5.0 中已弃用,因此文档建议您使用 CFUUID

相反。您可以使用

获取 CFUUID
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFUUIDCreateString(NULL,uuidRef);
CFRelease(uuidRef);

请将 uuidString 保存在用户默认值或其他位置,因为您无法再次生成相同的 uuidString。

您也可以使用 ma​​c 地址 代替 how-can-i-programmatically-get-the-mac-address-of-an-iphone 希望对你有帮助。

【讨论】:

  • 实际上,请不要使用硬件标识符:它们遭受的隐私问题完全相同,导致 Apple 需要弃用并最终禁止使用 UUID。
  • 欢迎老兄。如果它解决了您的问题,请接受此答案:-)
【解决方案2】:

UDID 已弃用。从 2013 年 5 月 1 日起,Apple 不再批准任何访问 uniqueIdentifier 的应用程序。相反,您可以使用 ASIIDentifier 或 NSUUID。或 MAC 地址。

我在IDManager 类中采用了这种方法, 这是来自不同解决方案的集合。 KeyChainUtil 是一个从钥匙串中读取的包装器。

#define kBuggyASIID             @"00000000-0000-0000-0000-000000000000"

+ (NSString *) getUniqueID {
    if (NSClassFromString(@"ASIdentifierManager")) {
        NSString * asiID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
        if ([asiID compare:kBuggyASIID] == NSOrderedSame) {
            NSLog(@"Error: This device return buggy advertisingIdentifier.");
            return [IDManager getUniqueID];
        } else {
            return asiID;
        }

    } else {
        return [IDManager getUniqueUUID];
    }
}


+ (NSString *) getUniqueUUID {
    NSError * error;
    NSString * uuid = [KeychainUtils getPasswordForUsername:kBuyassUser andServiceName:kIdOgBetilngService error:&error];
    if (error) {
        NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
        return nil;
    }
    if (!uuid) {
        DLog(@"No UUID found. Creating a new one.");
        uuid = [IDManager GetUUID];
        uuid = [Util md5String:uuid];
        [KeychainUtils storeUsername:kBuyassUser andPassword:uuid forServiceName:kIdOgBetilngService updateExisting:YES error:&error];
        if (error) {
            NSLog(@"Error geting unique UUID for this device! %@", [error localizedDescription]);
            return nil;
        }
    }
    return uuid;
}

/* NSUUID is after iOS 6. */
+ (NSString *)GetUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

#pragma mark - MAC address
// Return the local MAC addy
// Courtesy of FreeBSD hackers email list
// Last fallback for unique identifier
+ (NSString *) getMACAddress
{
    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("Error: Memory allocation error\n");
        return NULL;
    }

    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2\n");
        free(buf); // Thanks, Remy "Psy" Demerest
        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;
}

【讨论】:

    猜你喜欢
    • 2011-08-12
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 2016-06-10
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多