KeychainSwiftCBridge.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import Security
  2. import Foundation
  3. import KeychainSwift // You might need to remove this import in your project
  4. /**
  5. This file can be used in your ObjC project if you want to use KeychainSwift Swift library.
  6. Extend this file to add other functionality for your app.
  7. How to use
  8. ----------
  9. 1. Import swift code in your ObjC file:
  10. #import "YOUR_PRODUCT_MODULE_NAME-Swift.h"
  11. 2. Use KeychainSwift in your ObjC code:
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. KeychainSwiftCBridge *keychain = [[KeychainSwiftCBridge alloc] init];
  15. [keychain set:@"Hello World" forKey:@"my key"];
  16. NSString *value = [keychain get:@"my key"];
  17. 3. You might need to remove `import KeychainSwift` import from this file in your project.
  18. */
  19. @objcMembers public class KeychainSwiftCBridge: NSObject {
  20. let keychain = KeychainSwift()
  21. open var lastResultCode: OSStatus {
  22. get { return keychain.lastResultCode }
  23. }
  24. open var accessGroup: String? {
  25. set { keychain.accessGroup = newValue }
  26. get { return keychain.accessGroup }
  27. }
  28. open var synchronizable: Bool {
  29. set { keychain.synchronizable = newValue }
  30. get { return keychain.synchronizable }
  31. }
  32. @discardableResult
  33. open func set(_ value: String, forKey key: String) -> Bool {
  34. return keychain.set(value, forKey: key)
  35. }
  36. @discardableResult
  37. open func setData(_ value: Data, forKey key: String) -> Bool {
  38. return keychain.set(value, forKey: key)
  39. }
  40. @discardableResult
  41. open func setBool(_ value: Bool, forKey key: String) -> Bool {
  42. return keychain.set(value, forKey: key)
  43. }
  44. open func get(_ key: String) -> String? {
  45. return keychain.get(key)
  46. }
  47. open func getData(_ key: String) -> Data? {
  48. return keychain.getData(key)
  49. }
  50. open func getBool(_ key: String) -> Bool? {
  51. return keychain.getBool(key)
  52. }
  53. @discardableResult
  54. open func delete(_ key: String) -> Bool {
  55. return keychain.delete(key);
  56. }
  57. @discardableResult
  58. open func clear() -> Bool {
  59. return keychain.clear()
  60. }
  61. }