UDID被弃用了怎么办

苹果公司在iOS5将获取udid的api弃用,所有使用此方法的app将被拒绝发布到app store.
那么还有什么办法获取一个唯一的id呢?

方法1:

此为获取设备Mac地址方法,可以到git看看.(英文文档)
https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5

Description
Apple stopped supporting a unique identifier for iOS. This source code solves the problem. It generates a unique identifier based on the mac address of the device in combination with the bundle identifier.

What you need to do:

copy NSString+MD5Addition and UIDevice+IdentifierAddition to your project.
use [[UIDevice currentDevice] uniqueDeviceIdentifier] to retrieve the unique identifier or
use [[UIDevice currentDevice] uniqueGlobalDeviceIdentifier] to retrieve a global unique identifier (used for tracking between different apps).
have fun and follow twitter.com/gekitz 😉
//Thanks to Erica Sadun for her UIDevice+Hardware Addition (used for the mac address retrieval).

License
see license file.

这是可以使用的两种方法.
uniqueDeviceIdentifier (MD5 of MAC+CFBundleIdentifier)
uniqueGlobalDeviceIdentifier(MD5 of the MAC)

#import "UIDevice+IdentifierAddition.h"

NSLog(@"%@",[[UIDevice currentDevice] uniqueDeviceIdentifier]);
NSLog(@"%@",[[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]);

文件下载地址

方法2:

直接赤裸裸的使用用户设备MAC地址,使用原生的C语言.

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <ifaddrs.h>


char*  getMacAddress(char* macAddress, char* ifName) {

int  success;
struct ifaddrs * addrs;
struct ifaddrs * cursor;
const struct sockaddr_dl * dlAddr;
const unsigned char* base;
int i;

success = getifaddrs(&addrs) == 0;
if (success) {
    cursor = addrs;
    while (cursor != 0) {
        if ( (cursor->ifa_addr->sa_family == AF_LINK)
            && (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type == IFT_ETHER) && strcmp(ifName,  cursor->ifa_name)==0 ) {
            dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
            base = (const unsigned char*) &dlAddr->sdl_data[dlAddr->sdl_nlen];
            strcpy(macAddress, ""); 
            for (i = 0; i < dlAddr->sdl_alen; i++) {
                if (i != 0) {
                    strcat(macAddress, ":");
                }
                char partialAddr[3];
                sprintf(partialAddr, "%02X", base[i]);
                strcat(macAddress, partialAddr);

            }
        }
        cursor = cursor->ifa_next;
    }

    freeifaddrs(addrs);
}    
return macAddress;
}

接下来用Objc的正常方法来获取en0:

char* macAddressString= (char*)malloc(18);
NSString* macAddress= [[NSString alloc] initWithCString:getMacAddress(macAddressString,"en0")
                                              encoding:NSMacOSRomanStringEncoding];

方法3:

这个是苹果官方提供的一个决绝方案.
每次都临时生成一个随机的ID,我们叫它UUID.
在首次运行app后生成次id,并且将它保存在一个文件中,或者keychain中,以便下次运行程序时读取.
这样做的目的是:卸载,再安装程序后,这个id会发生变化.仅此而已.

+ (NSString *)GetUUID // 一个简单的类方法,用来生成一个随机的UUID
{ 
  CFUUIDRef theUUID = CFUUIDCreate(NULL); 
  CFStringRef string = CFUUIDCreateString(NULL, theUUID); 
  CFRelease(theUUID); 
  return [(NSString *)string autorelease]; 
}

总结:

话说道高一尺魔高一丈,总会有办法获取一个唯一的id.
当初苹果为自家的每个产品都设置一个udid的目的并不是让不法分子在不经用户许可的前提下做非法的事情.
(比如说做个市场调查什么的,哎呦,不好意思,我怎么给说出来了)
其实,开发人员获取设备信息以判断该执行什么样的的功能是尤其重要的.(尤其是游戏开发)
只是,老美对隐私的重视程度简直超过了我们对吃的重视程度.苹果也是压力山大~

总之,方法还是有的,怎么用那就还看程序员了.

发表评论

电子邮件地址不会被公开。 必填项已用*标注