2016-12-07 21 views
4

我想知道,如何使用Objective-C如何使用Objective-C

,這樣產生的iPhone/iPad設備的唯一ID一旦安裝應用程序生成的iPhone/iPad設備的唯一ID在設備上,所以我們應該跟蹤我已搜查檢索iPhone/iPad上的IMEI是設備ID

  • ,但它不是在Objective-C允許的。

  • 然後我搜索到了生成iPhone/iPad的UDID,但每次我在模擬器上啓動它時,它都會生成不同的ID。

+0

http://stackoverflow.com/a/39992564/6656894參考此答案 –

+0

的可能的複製[唯一標識符的UIDevice棄用 - 什麼現在要做的(http://stackoverflow.com/questions/6993325/uidevice -uniqueidentifier-deprecated-what-to-do-now) – Venkat

回答

5

是,UDID被棄用;由於用戶隱私的目的,我們不允許獲得UDID。蘋果不允許獲得唯一標識設備的任何標識符,如IMEI,MAC地址,UDID

UUID是去截至目前的最佳途徑。但是這對每個供應商都是獨一無二的。您無法保證每次獲得字符串時它都是唯一的。最好的選擇是將UUID字符串存儲到手機的鑰匙扣,並在需要時將其抓回來。當您在出廠時重置手機時,鑰匙鏈項目將被刪除。應該記住這個限制。


更新 - 在IOS 10.3 BETA'S:

看來,蘋果已經在iOS版10.3+做了一些改動如何鑰匙扣作品。存儲在鑰匙串中的鑰匙串項目將被刪除刪除當來自特定供應商的所有應用程序都被卸載。據蘋果稱,即使應用程序從設備中消失,應用程序敏感信息的駐留也可能導致安全風險,因此他們決定禁止這種行爲。

即使在應用程序卸載後依賴於鑰匙串存儲的開發人員也可以使用此WORKAROUND繼續使用預期的功能。根據此解決方法,任何應用程序都可以訪問存儲在該特定鑰匙串訪問組中的信息,因此建議您向數據添加額外的加密層以保護其更加安全,儘管鑰匙串默認情況下會加密項目。


更新 - IOS 10.3.3(STABLE): 看來,鑰匙串項缺失是iOS中10.3.3的早期測試版的一個BUG,後來在穩定版本是固定的。這可能是在貝塔斯期間引起的,因爲在這個階段會發生奇怪的事情。以後使用鑰匙串應該沒有問題。

+1

這是10.3測試版中的一個錯誤,而卸載應用程序正在刪除它的鑰匙串數據,但現在情況並非如此。 [見這裏](https://stackoverflow.com/a/43063683/3411787) –

+0

@ZaidKhan是的,我見過那個帖子。是否有任何官方確認或文件說明再次正常工作?但我會更新我的答案。但100%的保證不能從它或開發者論壇中拿走。 –

+0

@BadhanGanesh我不認爲有任何官方文檔顯示10.3將刪除鑰匙串數據,所以不會有你要求的文檔。沒有? –

6

您可以使用UUID(通用用戶標識)。下面的鏈接包含蘋果文檔

https://developer.apple.com/reference/uikit/uidevice/1620059-identifierforvendor

https://developer.apple.com/library/content/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW1

您可以使用此代碼UUID:

// Objective-C的

NSString * string = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 

//斯威夫特

let deviceID = UIDevice.currentDevice().identifierForVendor?.UUIDString 

//斯威夫特3

let deviceID = UIDevice.current.identifierForVendor?.uuidString 
+1

設備是否是唯一的?如果我刪除一個應用程序,然後重新安裝它,它會一樣嗎? – Xeieshan

+0

不,它不會是一樣的。當你刪除並重新安裝時,它會重新生成一個新的。 –

4

使用下面的代碼獲取UDID的iOS設備使用 類KeychainItemWrapper從網址下載 KeychainItemWrap

NSString *uuid; 
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"AC_APP_UUID" accessGroup:nil]; 
NSString *keychainUUID = [keychain objectForKey:(__bridge id)(kSecAttrAccount)]; 
NSString *appVersion = [NSString stringWithFormat:@"%@",@"1.0"]; 
[keychain setObject:appVersion forKey:(__bridge id)(kSecAttrDescription)]; 
if (keychainUUID==nil||[keychainUUID isKindOfClass:[NSNull class]]||keychainUUID.length==0) { 
    uuid = [[NSUUID UUID] UUIDString]; 
    [keychain setObject:uuid forKey:(__bridge id)(kSecAttrAccount)]; 
}else{ 
    uuid = [keychain objectForKey:(__bridge id)(kSecAttrAccount)]; 
} 
+0

它被Apple App Review團隊接受嗎? –

+1

是的,這是我的應用程序在appstore上。 – SM18

+0

非常感謝朋友@Sumit –

-3

解決辦法是:

你可以用DeviceToken做到這一點。 DeviceToken是所有移動設備的uniq。

代碼是在這裏:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) 
    { 
     [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 

     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } 
    else 
    { 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
    } 
    return yes; 

} 


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 

    NSString *AppDeviceToken=[[NSString alloc] initWithFormat:@"%@",deviceToken]; 
    //NSLog(@"My token is: %@", self.AppDeviceToken); 

    AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
    AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@">" withString:@""]; 

    NSLog(@"%@'s Device Token is : %@",[[UIDevice currentDevice] name],AppDeviceToken); 
} 
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error 
{ 
    NSLog(@"Failed to get token, error: %@", error); 
} 

設備令牌是uniq的所有設備。

+1

設備ID可以更改,如果您刪除應用程序並再次安裝在同一設備上。 – Wolverine

+1

是的,@金剛狼你是對的。 – sohil

-1

您不能攜帶用戶手機的IMEI號碼和手機號碼,Apple限制只能獲取這些uniqueID號碼。

您必須將UDID存儲在鑰匙串中。爲此,你必須下載keychainwrapper類和存儲由上面的代碼生成的UDID:

UIDevice *device = [[UIDevice alloc]init]; 
    NSString *idForVend = [NSString stringWithFormat:@"%@", [device identifierForVendor]]; 
    NSLog(@"Identifier For Vendor : %@",idForVend); 

請點擊此鏈接會解決你的問題是肯定的:https://stackoverflow.com/questions/16459879/how-to-store-a-string-in-keychain-ios,iOS的? 如果你使用這個如果你刪除應用程序並再次安裝它將保持相同的DeviceID。

-1
#import <AdSupport/AdSupport.h> 

NSString* sClientID = [[[ASIdentifierManager sharedManager]advertisingIdentifier] UUIDString]; 

最好的UUID因爲:

  1. 它永遠不會改變(*,即使應用程序將被刪除)
  2. 蘋果批准。
+0

錯了。該文檔說'advertIdentifier' [「可能會更改 - 例如,如果用戶擦除設備」](https://developer.apple.com/documentation/adsupport/asidentifiermanager/1614151-advertisingidentifier?language=objc)。我相信也可以通過轉到設置>隱私>廣告>重置廣告標識符來更改它。此外,[「在iOS 10.0及更高版本中,當用戶限制廣告跟蹤時,advertisingIdentifier的值全部爲零」](https://developer.apple.com/documentation/adsupport/asidentifiermanager/1614151-advertisingidentifier?language=objc )所以它不是唯一的。 – Pang

+0

@Pang你有解決方案嗎? –