如何为IOS App创建唯一ID

时间:2017-03-07 06:36:40

标签: ios

我正在创建一个ios应用程序,因为不推荐使用udid我们如何唯一地识别应用程序。

2 个答案:

答案 0 :(得分:1)

您可以同时使用UDID和& UUID 对于UUID- let uuid = NSUUID().uuidString 对于UDID- let Identifier = UIDevice.current.identifierForVendor?.uuidString

答案 1 :(得分:1)

以下代码为设备生成唯一ID并且IT IS UNIQUE,您将重置设备。快乐的编码...... :)

- (NSString *)createNewUUID {
    NSString *UUID = [[NSUUID UUID] UUIDString];
    return UUID;
}


//Check And Retrive UniqueIdentifier
-(void)checkAndGenerateUniqueIdentifier{

    //Retrive UDID
    NSString *retrieveuuid = [SSKeychain passwordForService:[[NSBundle mainBundle] bundleIdentifier] account:[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleNameKey]];

    if (retrieveuuid == nil) {

        NSString *uuid = [self createNewUUID];
        NSLog(@" Create new uuid : %@",uuid);

        //Store the password in Keychain
        NSError *error = nil;

        [SSKeychain setPassword:uuid forService:[[NSBundle mainBundle] bundleIdentifier] account:[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleNameKey] error:&error];

        if ([error code] == SSKeychainErrorNotFound) {
            NSLog(@"Password not found");
        }

    }

}

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

    //Check And Retrive UniqueIdentifier
    [self checkAndGenerateUniqueIdentifier];

    return YES;
}

使用第三方钥匙串包装或iOS Keychain API for Store UUID。