Skip to content

Commit

Permalink
feat: getImage iOS (#253)
Browse files Browse the repository at this point in the history
* feat: getImage iOS

* Support more image types including GIF

* use itemProviders

* use identifier that comforms to UTTypeImage
  • Loading branch information
s77rt authored Nov 1, 2024
1 parent 239948a commit 9e152fe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ _setContent() {
| ------- | -------- | -------- | ----------------------------------------- |
| content | string[] | Yes | The content to be stored in the clipboard |


#### `hasString()`

```jsx
Expand Down Expand Up @@ -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.
Expand All @@ -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() {
Expand Down
20 changes: 18 additions & 2 deletions ios/RNCClipboard.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "RNCClipboard.h"


#import <MobileCoreServices/MobileCoreServices.h>
#import <MobileCoreServices/UTType.h>
#import <UIKit/UIKit.h>
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/NativeClipboardModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface Spec extends TurboModule {
*/
setImage(content: string): Promise<void>;
/**
* (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() {
Expand Down

0 comments on commit 9e152fe

Please sign in to comment.