diff --git a/README.md b/README.md index 7528294f..81a83a40 100644 --- a/README.md +++ b/README.md @@ -215,7 +215,6 @@ _setContent() { | ------- | -------- | -------- | ----------------------------------------- | | content | string[] | Yes | The content to be stored in the clipboard | - #### `hasString()` ```jsx @@ -255,8 +254,6 @@ static hasWebURL() Returns whether the clipboard has a WebURL(UIPasteboardDetectionPatternProbableWebURL) content. Can check if there is a WebURL content in clipboard without triggering PasteBoard notification for iOS 14+ - - ### useClipboard `useClipboard` is a utility hooks for the `Clipboard` module. `data` contains the content stored in the clipboard. @@ -283,7 +280,7 @@ export const HooksSample = () => { static getImage() ``` -Get content of image in base64 string type, this method returns a `Promise`, so you can use following code to get clipboard content (ANDROID only) +Get content of image in base64 string type, this method returns a `Promise`, so you can use following code to get clipboard content (iOS and Android Only) ```jsx async _getContent() { diff --git a/ios/RNCClipboard.mm b/ios/RNCClipboard.mm index d43a32d0..3ee5caa1 100644 --- a/ios/RNCClipboard.mm +++ b/ios/RNCClipboard.mm @@ -1,6 +1,7 @@ #import "RNCClipboard.h" - +#import +#import #import #import #import @@ -215,7 +216,22 @@ - (void) listener:(NSNotification *) notification RCT_EXPORT_METHOD(getImage:(RCTPromiseResolveBlock)resolve reject:(__unused RCTPromiseRejectBlock)reject){ - reject(@"Clipboard:getImage", @"getImage is not supported on iOS", nil); + UIPasteboard *clipboard = [UIPasteboard generalPasteboard]; + NSString *withPrefix; + for (NSItemProvider *itemProvider in clipboard.itemProviders) { + if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) { + for (NSString *identifier in itemProvider.registeredTypeIdentifiers) { + if (UTTypeConformsTo((__bridge CFStringRef)identifier, kUTTypeImage)) { + NSString *MIMEType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)identifier, kUTTagClassMIMEType); + NSString *imageDataBase64 = [[clipboard dataForPasteboardType:identifier] base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; + withPrefix = [NSString stringWithFormat:@"data:%@;base64,%@", MIMEType, imageDataBase64]; + break; + } + } + break; + } + } + resolve((withPrefix ? : NULL)); } RCT_EXPORT_METHOD(addListener : (NSString *)eventName) { diff --git a/src/Clipboard.ts b/src/Clipboard.ts index cbae1516..9de2f0fe 100644 --- a/src/Clipboard.ts +++ b/src/Clipboard.ts @@ -71,7 +71,7 @@ export const Clipboard = { NativeClipboard.setImage(content); }, /** - * (Android Only) + * (iOS and Android Only) * Get clipboard image in base64, this method returns a `Promise`, so you can use following code to get clipboard content * ```javascript * async _getContent() { diff --git a/src/NativeClipboardModule.ts b/src/NativeClipboardModule.ts index bb9a2aa0..0a127141 100644 --- a/src/NativeClipboardModule.ts +++ b/src/NativeClipboardModule.ts @@ -59,7 +59,7 @@ export interface Spec extends TurboModule { */ setImage(content: string): Promise; /** - * (Android Only) + * (iOS and Android Only) * Get clipboard image in base64, this method returns a `Promise`, so you can use following code to get clipboard content * ```javascript * async _getContent() {