KeyChainPlugin.mm 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #import "KeyChainPlugin.h"
  2. #import "UICKeyChainStore.h"
  3.  
  4. NSString *_keyForID = @"UserID";
  5. NSString *_keyForUUID = @"UserUUID";
  6.  
  7. @implementation KeyChainPlugin
  8.  
  9. extern "C" {
  10.     char* getKeyChainUser();
  11.     void setKeyChainUser(const char* userId, const char* uuid);
  12.     void deleteKeyChainUser();
  13. }
  14.  
  15. char* getKeyChainUser()
  16. {
  17.     NSString *userId = [UICKeyChainStore stringForKey:_keyForID];
  18.     NSString *userUUID = [UICKeyChainStore stringForKey:_keyForUUID];
  19.  
  20.     if (userId == nil || [userId isEqualToString:@""]) {
  21.         NSLog(@"No user information");
  22.         userId = @"";
  23.         userUUID = @"";
  24.     }
  25.  
  26.     NSString* json = [NSString stringWithFormat:@"{\"userId\":\"%@\",\"uuid\":\"%@\"}",userId,userUUID];
  27.  
  28.     return makeStringCopy([json UTF8String]);
  29. }
  30.  
  31. void setKeyChainUser(const char* userId, const char* uuid)
  32. {
  33.     NSString *nsUseId = [NSString stringWithCString: userId encoding:NSUTF8StringEncoding];
  34.     NSString *nsUUID = [NSString stringWithCString: uuid encoding:NSUTF8StringEncoding];
  35.  
  36.     [UICKeyChainStore setString:nsUseId forKey:_keyForID];
  37.     [UICKeyChainStore setString:nsUUID forKey:_keyForUUID];
  38. }
  39.  
  40. void deleteKeyChainUser()
  41. {
  42.     [UICKeyChainStore removeItemForKey:_keyForID];
  43.     [UICKeyChainStore removeItemForKey:_keyForUUID];
  44. }
  45.  
  46. char* makeStringCopy(const char* str)
  47. {
  48.     if (str == NULL) {
  49.         return NULL;
  50.     }
  51.  
  52.     char* res = (char*)malloc(strlen(str) + 1);
  53.     strcpy(res, str);
  54.     return res;
  55. }
  56.  
  57. @end