[英]What to use if not “IPHONE UDID”?
哇...本周在線查看關於使用iPhone的UDID的所有“恐慌故事”。
[[UIDevice currentDevice] uniqueIdentifier]
我們應該怎么使用呢?
如果手機被賣給另一個用戶......並且應用程序已根據手機的UDID在遠程服務器上存儲了一些數據,該怎么辦?
(當然,我想避免應用商店的“加密限制”問題。)
為什么不使用Mac地址,然后可能將其哈希。
有一位優秀的UIDevice拓類別這里
- (NSString *) macaddress
{
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("Could not allocate memory. error!\n");
return NULL;
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 2");
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)];
// 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;
}
你可以用模型散列這個嗎?
1-首先,正如Apple所建議的那樣,確定每次安裝而不是識別每個設備。 因此,您可以使用CFUUIDRef。 示例:
NSString *uuid = nil;
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
uuid = NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID));
[uuid autorelease];
CFRelease(theUUID);
}
2-如果您關心全球唯一標識符,則可以將此標識符存儲在iCloud上。
3-最后,如果你確實需要一個在重新安裝app之后仍然存在的標識符(這種情況不常發生),你可以使用Keychains( Apple的keychain doc )。 但蘋果團隊會喜歡嗎?
像這樣:
@interface UIDevice (UIDeviceAppIdentifier)
@property (readonly) NSString *deviceApplicationIdentifier;
@end
@implementation UIDevice (UIDeviceAppIdentifier)
- (NSString *) deviceApplicationIdentifier
{
static NSString *name = @"theDeviceApplicationIdentifier";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *value = [defaults objectForKey: name];
if (!value)
{
value = (NSString *) CFUUIDCreateString (NULL, CFUUIDCreate(NULL));
[defaults setObject: value forKey: name];
[defaults synchronize];
}
return value;
}
@end
iOS文檔或多或少地描述了使用CFUUIDCreate()來創建標識符並建議使用UserDefaults來存儲它。
UUID剛剛折舊,因此將會存在一段時間,Apple還沒有多說這種折舊,我會等到他們對此有更多的說法,也許會提供一些替代方案。
推薦的方法是使用UUID生成,並將其與用戶自己願意為應用程序提供的內容相關聯 。
然后,將此數據存儲在外部,以便再次檢索。 可能還有其他方法可以輕松完成此操作,但這是推薦的方法。
一種解決方案是讓應用程序發布免費的應用程序內購買。
此次購買將是:
可追蹤,具有唯一編號(購買)編號,僅對您的應用有意義。
如果此人切換設備,則可移動
可檢索,如果應用程序被刪除(或手機被擦除並重新加載) - 可以恢復應用程序內購買。
Apple的文檔說:
“不要使用uniqueIdentifier屬性。要創建特定於應用程序的唯一標識符,可以調用CFUUIDCreate函數創建UUID,並使用NSUserDefaults類將其寫入默認數據庫。”
這是一個快速片段:
CFUUIDRef udid = CFUUIDCreate(NULL);
NSString *udidString = (NSString *) CFUUIDCreateString(NULL, udid);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.