iOS取得装置の一意識別の実現ステップ
1.よく使うUID
UDIDは40ビットの16進数シーケンスであり、移動ネットワークはUDIDを利用してモバイルデバイスを識別することができる。
多くの開発者はUDIDをユーザーの個人情報と関連させて、ネットワークのスヌープ者は複数のアプリケーションからこれらのデータを収集して、ついでにこの人の多くのプライバシーデータを獲得します。同時に、ほとんどのアプリケーションはUDIDと個人情報を頻繁に伝送しています。集団訴訟を避けるため、アップルはiOS 5でこの慣例を廃止することにした。
UUIDを取得する方法:
MACアドレスは、インターネット上の各局の表示子を表すために用いられ、6バイト(48ビット)の16進数シーケンスである。前の3バイトはIEEEの登録管理機構RAが各メーカーに割り当てを担当している「作成上唯一のマーカー」であり、後の3バイトは各メーカーが生産するアダプターインターフェースに割り当てている。
MACアドレスはネットワーク上でデバイスの一意性を区別するために使われています。アクセスネットワークのデバイスはMACアドレスを持っています。一つのiPhoneには複数のMACアドレスがあります。WIFI、SIMなどが含まれていますが、iTouchとiPadにはWIFIがありますので、WIFIのMACアドレスを取得すればいいです。一般的にMD 5(MACアドレス+bundleID)を用いて一意の識別子を取得する。
しかし、MACアドレスはUDIDと同じで、プライバシー問題があります。iOS 7以降、全てのデバイスがMACアドレスを要求すると固定値が戻ります。
MACを取得することは、githubで良い方法を見つけることです。
2.1まず次のライブラリを導入する:
2.2新規ファイルを作成し、NSObjectを継承し、mファイルにヘッダファイルを導入し、マクロを定義します。
3.UID+自分で保存する
3.1 UIDを取得する二つの方法
3.2まずプロジェクトにKeyCharin Sharingを追加する
3.3サードパーティライブラリSecurity.frame eworkを導入する
3.4コアコード(コードが多い)
githubでSSKeychainを検索すれば見つけられます。SSKeychain.hとSSKeychain.mファイルだけでいいです。
UDIDは40ビットの16進数シーケンスであり、移動ネットワークはUDIDを利用してモバイルデバイスを識別することができる。
多くの開発者はUDIDをユーザーの個人情報と関連させて、ネットワークのスヌープ者は複数のアプリケーションからこれらのデータを収集して、ついでにこの人の多くのプライバシーデータを獲得します。同時に、ほとんどのアプリケーションはUDIDと個人情報を頻繁に伝送しています。集団訴訟を避けるため、アップルはiOS 5でこの慣例を廃止することにした。
UUIDを取得する方法:
/** */
+ (NSString *)getUUID{
return [UIDevice currentDevice].identifierForVendor.UUIDString;;
}
2.MACアドレスMACアドレスは、インターネット上の各局の表示子を表すために用いられ、6バイト(48ビット)の16進数シーケンスである。前の3バイトはIEEEの登録管理機構RAが各メーカーに割り当てを担当している「作成上唯一のマーカー」であり、後の3バイトは各メーカーが生産するアダプターインターフェースに割り当てている。
MACアドレスはネットワーク上でデバイスの一意性を区別するために使われています。アクセスネットワークのデバイスはMACアドレスを持っています。一つのiPhoneには複数のMACアドレスがあります。WIFI、SIMなどが含まれていますが、iTouchとiPadにはWIFIがありますので、WIFIのMACアドレスを取得すればいいです。一般的にMD 5(MACアドレス+bundleID)を用いて一意の識別子を取得する。
しかし、MACアドレスはUDIDと同じで、プライバシー問題があります。iOS 7以降、全てのデバイスがMACアドレスを要求すると固定値が戻ります。
MACを取得することは、githubで良い方法を見つけることです。
2.1まず次のライブラリを導入する:
2.2新規ファイルを作成し、NSObjectを継承し、mファイルにヘッダファイルを導入し、マクロを定義します。
#import "XWGetMAC.h"
#import <ifaddrs.h>
#import <resolv.h>
#import <arpa/inet.h>
#import <net/if.h>
#import <netdb.h>
#import <netinet/ip.h>
#import <net/ethernet.h>
#import <net/if_dl.h>
#define MDNS_PORT 5353
#define QUERY_NAME "_apple-mobdev2._tcp.local"
#define DUMMY_MAC_ADDR @"02:00:00:00:00:00"
#define IOS_CELLULAR @"pdp_ip0"
#define IOS_WIFI @"en0"
#define IOS_VPN @"utun0"
#define IP_ADDR_IPv4 @"ipv4"
#define IP_ADDR_IPv6 @"ipv6"
+ (NSString *)getMAC:(BOOL)preferIPv4 {
return [[XWGetMAC alloc] getIPAddress:preferIPv4];
}
/*
* IP
*/
- (NSString *)getIPAddress:(BOOL)preferIPv4
{
NSArray *searchArray = preferIPv4 ?
@[ IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
@[ IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;
NSDictionary *addresses = [self getIPAddr];
__block NSString *address;
[searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL * _Nonnull stop) {
address = addresses[key];
// IP
if([self isValidatIP:address]) *stop = YES;
}];
return address ? address : @"0.0.0.0";
}
- (BOOL)isValidatIP:(NSString *)ipAddress {
if (ipAddress.length == 0) {
return NO;
}
NSString *urlRegEx = @"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlRegEx options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch=[regex firstMatchInString:ipAddress options:0 range:NSMakeRange(0, [ipAddress length])];
return firstMatch;
}
return NO;
}
- (NSDictionary *)getIPAddr
{
NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
// retrieve the current interfaces - returns 0 on success
struct ifaddrs *interfaces;
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
struct ifaddrs *interface;
for(interface=interfaces; interface; interface=interface->ifa_next) {
if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
continue; // deeply nested code harder to read
}
const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
NSString *type;
if(addr->sin_family == AF_INET) {
if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
type = IP_ADDR_IPv4;
}
} else {
const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
type = IP_ADDR_IPv6;
}
}
if(type) {
NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
addresses[key] = [NSString stringWithUTF8String:addrBuf];
}
}
}
// Free memory
freeifaddrs(interfaces);
}
return [addresses count] ? addresses : nil;
}
/*
*
*/
- (nullable NSString *)getMacAddress {
res_9_init();
int len;
//get currnet ip address
NSString *ip = [self currentIPAddressOf:IOS_WIFI];
if(ip == nil) {
fprintf(stderr, "could not get current IP address of en0
");
return DUMMY_MAC_ADDR;
}//end if
//set port and destination
_res.nsaddr_list[0].sin_family = AF_INET;
_res.nsaddr_list[0].sin_port = htons(MDNS_PORT);
_res.nsaddr_list[0].sin_addr.s_addr = [self IPv4Pton:ip];
_res.nscount = 1;
unsigned char response[NS_PACKETSZ];
//send mdns query
if((len = res_9_query(QUERY_NAME, ns_c_in, ns_t_ptr, response, sizeof(response))) < 0) {
fprintf(stderr, "res_search(): %s
", hstrerror(h_errno));
return DUMMY_MAC_ADDR;
}//end if
//parse mdns message
ns_msg handle;
if(ns_initparse(response, len, &handle) < 0) {
fprintf(stderr, "ns_initparse(): %s
", hstrerror(h_errno));
return DUMMY_MAC_ADDR;
}//end if
//get answer length
len = ns_msg_count(handle, ns_s_an);
if(len < 0) {
fprintf(stderr, "ns_msg_count return zero
");
return DUMMY_MAC_ADDR;
}//end if
//try to get mac address from data
NSString *macAddress = nil;
for(int i = 0 ; i < len ; i++) {
ns_rr rr;
ns_parserr(&handle, ns_s_an, 0, &rr);
if(ns_rr_class(rr) == ns_c_in &&
ns_rr_type(rr) == ns_t_ptr &&
!strcmp(ns_rr_name(rr), QUERY_NAME)) {
char *ptr = (char *)(ns_rr_rdata(rr) + 1);
int l = (int)strcspn(ptr, "@");
char *tmp = calloc(l + 1, sizeof(char));
if(!tmp) {
perror("calloc()");
continue;
}//end if
memcpy(tmp, ptr, l);
macAddress = [NSString stringWithUTF8String:tmp];
free(tmp);
}//end if
}//end for each
macAddress = macAddress ? macAddress : DUMMY_MAC_ADDR;
return macAddress;
}//end getMacAddressFromMDNS
- (nonnull NSString *)currentIPAddressOf: (nonnull NSString *)device {
struct ifaddrs *addrs;
NSString *ipAddress = nil;
if(getifaddrs(&addrs) != 0) {
return nil;
}//end if
//get ipv4 address
for(struct ifaddrs *addr = addrs ; addr ; addr = addr->ifa_next) {
if(!strcmp(addr->ifa_name, [device UTF8String])) {
if(addr->ifa_addr) {
struct sockaddr_in *in_addr = (struct sockaddr_in *)addr->ifa_addr;
if(in_addr->sin_family == AF_INET) {
ipAddress = [self IPv4Ntop:in_addr->sin_addr.s_addr];
break;
}//end if
}//end if
}//end if
}//end for
freeifaddrs(addrs);
return ipAddress;
}//end currentIPAddressOf:
- (nullable NSString *)IPv4Ntop: (in_addr_t)addr {
char buffer[INET_ADDRSTRLEN] = {0};
return inet_ntop(AF_INET, &addr, buffer, sizeof(buffer)) ?
[NSString stringWithUTF8String:buffer] : nil;
}//end IPv4Ntop:
- (in_addr_t)IPv4Pton: (nonnull NSString *)IPAddr {
in_addr_t network = INADDR_NONE;
return inet_pton(AF_INET, [IPAddr UTF8String], &network) == 1 ?
network : INADDR_NONE;
}//end IPv4Pton:
もし「_」が現れたらレスス9_nit「referenced from:」このエラーは、ステップ1のいくつかのライブラリを追加していないためです。3.UID+自分で保存する
3.1 UIDを取得する二つの方法
/** */
+ (NSString *)getUUID{
CFUUIDRef uuid = CFUUIDCreate(NULL);
NSString *UUID = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
return UUID;
}
/** */
+ (NSString *)getUUID{
return [UIDevice currentDevice].identifierForVendor.UUIDString;;
}
UUIDはデバイスの一意性をサポートするには十分ではないことは明らかです。現在多くのアプリには新しいユーザーの優待がありますが、デバイスごとにアカウントをバインドすることを保証します。UUIDを単純に使うと、もうこの需要を満たすことができません。だから、ここではkeychainで保存しなければなりません。3.2まずプロジェクトにKeyCharin Sharingを追加する
3.3サードパーティライブラリSecurity.frame eworkを導入する
3.4コアコード(コードが多い)
githubでSSKeychainを検索すれば見つけられます。SSKeychain.hとSSKeychain.mファイルだけでいいです。
#import <Foundation/Foundation.h>
#import <Security/Security.h>
/** Error codes that can be returned in NSError objects. */
typedef enum {
/** No error. */
SSKeychainErrorNone = noErr,
/** Some of the arguments were invalid. */
SSKeychainErrorBadArguments = -1001,
/** There was no password. */
SSKeychainErrorNoPassword = -1002,
/** One or more parameters passed internally were not valid. */
SSKeychainErrorInvalidParameter = errSecParam,
/** Failed to allocate memory. */
SSKeychainErrorFailedToAllocated = errSecAllocate,
/** No trust results are available. */
SSKeychainErrorNotAvailable = errSecNotAvailable,
/** Authorization/Authentication failed. */
SSKeychainErrorAuthorizationFailed = errSecAuthFailed,
/** The item already exists. */
SSKeychainErrorDuplicatedItem = errSecDuplicateItem,
/** The item cannot be found.*/
SSKeychainErrorNotFound = errSecItemNotFound,
/** Interaction with the Security Server is not allowed. */
SSKeychainErrorInteractionNotAllowed = errSecInteractionNotAllowed,
/** Unable to decode the provided data. */
SSKeychainErrorFailedToDecode = errSecDecode
} SSKeychainErrorCode;
extern NSString *const kSSKeychainErrorDomain;
/** Account name. */
extern NSString *const kSSKeychainAccountKey;
/**
Time the item was created.
The value will be a string.
*/
extern NSString *const kSSKeychainCreatedAtKey;
/** Item class. */
extern NSString *const kSSKeychainClassKey;
/** Item description. */
extern NSString *const kSSKeychainDescriptionKey;
/** Item label. */
extern NSString *const kSSKeychainLabelKey;
/** Time the item was last modified.
The value will be a string.
*/
extern NSString *const kSSKeychainLastModifiedKey;
/** Where the item was created. */
extern NSString *const kSSKeychainWhereKey;
/**
Simple wrapper for accessing accounts, getting passwords, setting passwords, and deleting passwords using the system
Keychain on Mac OS X and iOS.
This was originally inspired by EMKeychain and SDKeychain (both of which are now gone). Thanks to the authors.
SSKeychain has since switched to a simpler implementation that was abstracted from [SSToolkit](http://sstoolk.it).
*/
@interface SSKeychain : NSObject
///-----------------------
/// @name Getting Accounts
///-----------------------
/**
Returns an array containing the Keychain's accounts, or `nil` if the Keychain has no accounts.
See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
dictionaries returned by this method.
@return An array of dictionaries containing the Keychain's accounts, or `nil` if the Keychain doesn't have any
accounts. The order of the objects in the array isn't defined.
@see allAccounts:
*/
+ (NSArray *)allAccounts;
/**
Returns an array containing the Keychain's accounts, or `nil` if the Keychain doesn't have any
accounts.
See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
dictionaries returned by this method.
@param error If accessing the accounts fails, upon return contains an error that describes the problem.
@return An array of dictionaries containing the Keychain's accounts, or `nil` if the Keychain doesn't have any
accounts. The order of the objects in the array isn't defined.
@see allAccounts
*/
+ (NSArray *)allAccounts:(NSError **)error;
/**
Returns an array containing the Keychain's accounts for a given service, or `nil` if the Keychain doesn't have any
accounts for the given service.
See the `NSString` constants declared in SSKeychain.h for a list of keys that can be used when accessing the
dictionaries returned by this method.
@param serviceName The service for which to return the corresponding accounts.
@return An array of dictionaries containing the Keychain's accountsfor a given `serviceName`, or `nil` if the Keychain
doesn't have any accounts for the given `serviceName`. The order of the objects in the array isn't defined.
@see accountsForService:error:
*/
+ (NSArray *)accountsForService:(NSString *)serviceName;
/**
Returns an array containing the Keychain's accounts for a given service, or `nil` if the Keychain doesn't have any
accounts for the given service.
@param serviceName The service for which to return the corresponding accounts.
@param error If accessing the accounts fails, upon return contains an error that describes the problem.
@return An array of dictionaries containing the Keychain's accountsfor a given `serviceName`, or `nil` if the Keychain
doesn't have any accounts for the given `serviceName`. The order of the objects in the array isn't defined.
@see accountsForService:
*/
+ (NSArray *)accountsForService:(NSString *)serviceName error:(NSError **)error;
///------------------------
/// @name Getting Passwords
///------------------------
/**
Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't have a
password for the given parameters.
@param serviceName The service for which to return the corresponding password.
@param account The account for which to return the corresponding password.
@return Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't
have a password for the given parameters.
@see passwordForService:account:error:
*/
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account;
/**
Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't have a
password for the given parameters.
@param serviceName The service for which to return the corresponding password.
@param account The account for which to return the corresponding password.
@param error If accessing the password fails, upon return contains an error that describes the problem.
@return Returns a string containing the password for a given account and service, or `nil` if the Keychain doesn't
have a password for the given parameters.
@see passwordForService:account:
*/
+ (NSString *)passwordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
/**
Returns the password data for a given account and service, or `nil` if the Keychain doesn't have data
for the given parameters.
@param serviceName The service for which to return the corresponding password.
@param account The account for which to return the corresponding password.
@param error If accessing the password fails, upon return contains an error that describes the problem.
@return Returns a the password data for the given account and service, or `nil` if the Keychain doesn't
have data for the given parameters.
@see passwordDataForService:account:error:
*/
+ (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account;
/**
Returns the password data for a given account and service, or `nil` if the Keychain doesn't have data
for the given parameters.
@param serviceName The service for which to return the corresponding password.
@param account The account for which to return the corresponding password.
@param error If accessing the password fails, upon return contains an error that describes the problem.
@return Returns a the password data for the given account and service, or `nil` if the Keychain doesn't
have a password for the given parameters.
@see passwordDataForService:account:
*/
+ (NSData *)passwordDataForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
///-------------------------
/// @name Deleting Passwords
///-------------------------
/**
Deletes a password from the Keychain.
@param serviceName The service for which to delete the corresponding password.
@param account The account for which to delete the corresponding password.
@return Returns `YES` on success, or `NO` on failure.
@see deletePasswordForService:account:error:
*/
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account;
/**
Deletes a password from the Keychain.
@param serviceName The service for which to delete the corresponding password.
@param account The account for which to delete the corresponding password.
@param error If deleting the password fails, upon return contains an error that describes the problem.
@return Returns `YES` on success, or `NO` on failure.
@see deletePasswordForService:account:
*/
+ (BOOL)deletePasswordForService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
///------------------------
/// @name Setting Passwords
///------------------------
/**
Sets a password in the Keychain.
@param password The password to store in the Keychain.
@param serviceName The service for which to set the corresponding password.
@param account The account for which to set the corresponding password.
@return Returns `YES` on success, or `NO` on failure.
@see setPassword:forService:account:error:
*/
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account;
/**
Sets a password in the Keychain.
@param password The password to store in the Keychain.
@param serviceName The service for which to set the corresponding password.
@param account The account for which to set the corresponding password.
@param error If setting the password fails, upon return contains an error that describes the problem.
@return Returns `YES` on success, or `NO` on failure.
@see setPassword:forService:account:
*/
+ (BOOL)setPassword:(NSString *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
/**
Sets arbirary data in the Keychain.
@param password The data to store in the Keychain.
@param serviceName The service for which to set the corresponding password.
@param account The account for which to set the corresponding password.
@param error If setting the password fails, upon return contains an error that describes the problem.
@return Returns `YES` on success, or `NO` on failure.
@see setPasswordData:forService:account:error:
*/
+ (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account;
/**
Sets arbirary data in the Keychain.
@param password The data to store in the Keychain.
@param serviceName The service for which to set the corresponding password.
@param account The account for which to set the corresponding password.
@param error If setting the password fails, upon return contains an error that describes the problem.
@return Returns `YES` on success, or `NO` on failure.
@see setPasswordData:forService:account:
*/
+ (BOOL)setPasswordData:(NSData *)password forService:(NSString *)serviceName account:(NSString *)account error:(NSError **)error;
///--------------------
/// @name Configuration
///--------------------
#if __IPHONE_4_0 && TARGET_OS_IPHONE
/**
Returns the accessibility type for all future passwords saved to the Keychain.
@return Returns the accessibility type.
The return value will be `NULL` or one of the "Keychain Item Accessibility Constants" used for determining when a
keychain item should be readable.
@see accessibilityType
*/
+ (CFTypeRef)accessibilityType;
/**
Sets the accessibility type for all future passwords saved to the Keychain.
@param accessibilityType One of the "Keychain Item Accessibility Constants" used for determining when a keychain item
should be readable.
If the value is `NULL` (the default), the Keychain default will be used.
@see accessibilityType
*/
+ (void)setAccessibilityType:(CFTypeRef)accessibilityType;
#endif
@end
3.4新しいクラスを作成し、SSKeychainパッケージを参照する
#import "GetKeychain.h"
#import "SSKeychain.h"
@implementation GetKeychain
+ (NSString *)getDeviceUUID {
NSString *currentDeviceUUIDStr = [SSKeychain passwordForService:@" boudle id" account:@"uuid"];
if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""])
{
NSUUID *currentDeviceUUID = [UIDevice currentDevice].identifierForVendor;
currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
[SSKeychain setPassword: currentDeviceUUIDStr forService:@" boudle id" account:@"uuid"];
}
return currentDeviceUUIDStr;
}
@end
以上はiOS取得装置が一意に識別する実現ステップの詳細です。iOS取得装置の一意の識別に関する資料は他の関連記事に注目してください。