Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using KeychainAccess for saving UUID and store after reinstall app #192

Closed
LiborZ opened this issue Dec 14, 2015 · 4 comments
Closed

Using KeychainAccess for saving UUID and store after reinstall app #192

LiborZ opened this issue Dec 14, 2015 · 4 comments

Comments

@LiborZ
Copy link

LiborZ commented Dec 14, 2015

Hi,

I am looking solution for have same ID for device even when user delete app and then install it again. I found that others are using keychain for this. But I've read about some problems that sometimes Keychain returns nil and the second problem could be that ID from keychain would be shared across devices and I really don't want that. I guess that when I set synchronizable to false that there shoudn't be second issue. But what about first one? Is it possible that sometimes Keychain returns nil even if there are saved data? For example link here: soffes/SAMKeychain#75 Should I combine this with NSUserDefaults? When app is launched for first time get ID from keychain and save it to NSUserDefaults for other needs. Thanks for help

@kishikawakatsumi
Copy link
Owner

I think that Keychain returns nil is very rare case.

@LiborZ
Copy link
Author

LiborZ commented Jan 22, 2016

Ok, thanks for answer. My usage of KeychainAccess:

func getUUID() -> String {
    let userDefaults = NSUserDefaults.standardUserDefaults()
    if let uuid = userDefaults.objectForKey(Constants.UserDefaults.UUID) as? String {
      return uuid
    } else {
      let keychain = Keychain(service: Constants.Keychain.Service)
      if let token = try? keychain.getString(Constants.Keychain.UUID) {
        if let tkn = token {
          userDefaults.setObject(token, forKey: Constants.UserDefaults.UUID)
          return tkn
        } else {
          return  self.generateNewUUID()
        }

      } else {
        return self.generateNewUUID()
      }
    }
  }

  func generateNewUUID() -> String {
    let userDefaults = NSUserDefaults.standardUserDefaults()
    let keychain = Keychain(service: Constants.Keychain.Service)

    let UUID = NSUUID().UUIDString
    userDefaults.setObject(UUID, forKey: Constants.UserDefaults.UUID)
    userDefaults.synchronize()
    keychain[Constants.Keychain.UUID] = UUID
    return UUID
  }

If it is okay you can close this. I am use it this way and for now without problem.

@itsjaychang
Copy link

Hi @LiborZ

Did setting the synchronizable to false help prevent the Keychain from being shared among devices through iCloud?

@LiborZ
Copy link
Author

LiborZ commented Feb 22, 2016

Nope. Is it enought to change in both methods code to:

let keychain = Keychain(service: Constants.KeyChain.Service).synchronizable(false)

or do I need to do more changes? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants