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 it together with native iOS project #621

Open
CyberMew opened this issue Jan 4, 2024 · 2 comments
Open

Using it together with native iOS project #621

CyberMew opened this issue Jan 4, 2024 · 2 comments

Comments

@CyberMew
Copy link

CyberMew commented Jan 4, 2024

Is it possible to use this library on iOS natively? Instead of getting another separate native library, we could reuse this one on our native codes.

@micnem
Copy link

micnem commented Jan 31, 2024

I am interested in setting the credentials in React Native using this library and retrieving them on the native level using SecItemCopyMatching. Can anyone help with this?

@micnem
Copy link

micnem commented Feb 1, 2024

I am interested in setting the credentials in React Native using this library and retrieving them on the native level using SecItemCopyMatching. Can anyone help with this?

In answer to my own question, I implemented it this way, where service is equal to the tag you assigned when calling setGenericPassword

  
  let service = "ACCESS_TOKEN"

  let query: [String: Any] = [
      kSecClass as String: kSecClassGenericPassword,
      kSecAttrService as String: service,
      kSecReturnAttributes as String: kCFBooleanTrue!,
      kSecReturnData as String: kCFBooleanTrue!,
      kSecMatchLimit as String: kSecMatchLimitOne,
  ]

  var item: CFTypeRef?
  let status = SecItemCopyMatching(query as CFDictionary, &item)

  guard status != errSecItemNotFound else {
    print("Error", "Can't find key")
    return ""
  }

  guard status == errSecSuccess else {
      let error = NSError(domain: NSOSStatusErrorDomain, code: Int(status), userInfo: nil)
      print("Error", "Could not read keychain item", error)
      return ""
  }

  guard let existingItem = item as? [String: Any],
        let passwordData = existingItem[kSecValueData as String] as? Data,
        let password = String(data: passwordData, encoding: String.Encoding.utf8),
        let account = existingItem[kSecAttrAccount as String] as? String else {
      print("Error", "Unexpected keychain data")
      return ""
  }

  return password
}

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

No branches or pull requests

2 participants