Skip to content

Instantly share code, notes, and snippets.

@eugenehp
Created April 5, 2021 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugenehp/9685202ed00b2d0aaf4d403ff583bb2c to your computer and use it in GitHub Desktop.
Save eugenehp/9685202ed00b2d0aaf4d403ff583bb2c to your computer and use it in GitHub Desktop.
react-native-randomness Objective-C code
#import "Randomness.h"
@implementation Randomness
RCT_EXPORT_MODULE()
// Example method
// See // https://reactnative.dev/docs/native-modules-ios
RCT_REMAP_METHOD(random,
randomWithLength:(nonnull NSNumber*)length
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
NSMutableData *data = [NSMutableData dataWithLength:[length integerValue]];
int result = SecRandomCopyBytes(kSecRandomDefault, [length integerValue], data.mutableBytes);
if (result != errSecSuccess) {
NSError *error = [[NSError alloc] initWithDomain:NSPOSIXErrorDomain
code:result userInfo:nil];
reject(@"error", @"Failed to generate random bytes using secure methods.", error);
}
NSLog(@"Generated random data, %@", data);
resolve([data base64EncodedStringWithOptions:0]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment