Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: getImage iOS #253

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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