React Native Clipboard - how to copy an image or something other than text?

In React Native, with clipboard, how to place image on clipboard? The only method provided for setting the contents of the clipboard is "setString". Can you set images or other content besides strings?

+4
source share
1 answer

You can link your own iOS clipboard API and set the method setImage. For this you need:

  • Add your own module header file Clipboard.h:

#import "RCTBridgeModule.h"
@interface Clipboard : NSObject <RCTBridgeModule>
@end
Run code
  1. Clipboard.m. base64, , :

#import <UIKit/UIKit.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import "Clipboard.h"

@implementation Clipboard

RCT_EXPORT_MODULE(BetterClipboard); // this is how our native module will be named

RCT_EXPORT_METHOD(addBase64Image:(NSString *)base64Image) {
  UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  [pasteboard setPersistent:YES];
  
  NSData *imageData = [[NSData alloc]initWithBase64EncodedString:base64Image options:NSDataBase64DecodingIgnoreUnknownCharacters];

  [pasteboard setImage:[UIImage imageWithData:imageData]];
}

@end
  1. React:

import { NativeModules } from 'react-native';
   
NativeModules.BetterClipboard.addBase64Image(base64EncodedImage);

, , Android.

+4

Source: https://habr.com/ru/post/1678474/


All Articles