简体   繁体   English

如何在iPhone SDK中对设备进行审核?

[英]how to get device udid in iphone sdk?

I developing iPhone application. 我正在开发iPhone应用程序。 In this app i need to get device udid. 在此应用中,我需要对设备进行审核。 In simulator working fine, but in iphone device not getting correct value, the values getting 0. 在模拟器中工作正常,但在iPhone设备中未获得正确的值,该值变为0。

The code are following; 代码如下;

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

Thanks, 谢谢,

As we know uniqueIdentifier is deprecated in iOS 5.0 so docs recommend you to use CFUUID 我们知道iOS 5.0中不推荐使用uniqueIdentifier,因此文档建议您使用CFUUID

instead. 代替。 You can get CFUUID using 您可以使用获取CFUUID

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

Please save the uuidString in user defaults or in other place because you can not generate the same uuidString again. 请将uuidString保存在用户默认设置或其他位置,因为您无法再次生成相同的uuidString。

You can use mac address also in place of this as an alternative how-can-i-programmatically-get-the-mac-address-of-an-iphone Hope it helps you. 您也可以使用mac地址代替它,作为一种替代方法, 我可以通过编程方式获取iphone的mac地址,希望对您有所帮助。

UDID is deprecated. UDID已弃用。 From 1st May 2013, Apple is not approving any app that access uniqueIdentifier. 从2013年5月1日起,Apple不会批准任何可访问uniqueIdentifier的应用程序。 Instead you can use ASIIDentifier or NSUUID.Or MAC address. 相反,您可以使用ASIIDentifier或NSUUID。或MAC地址。

I have followed this approach in IDManager class, This is a collection from different solutions. 我在IDManager类中遵循了这种方法,这是来自不同解决方案的集合。 KeyChainUtil is a wrapper to read from keychain. 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM