React Native-剪贴板中更改的侦听器 [英] React Native - Listener for change in clipboard

查看:240
本文介绍了React Native-剪贴板中更改的侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以为React Native中的剪贴板数据更改添加侦听器? 基本上取决于用户是否已在剪贴板中复制了某些内容,无论是在应用程序内部还是在后台应用程序,我都想执行一些方法.

Is there a way to add a listener for a change in clipboard data in React Native? Basically depending on whether the user has copied something in their clipboard, regardless of whether inside the app or with the app in the background, I want to perform some methods.

推荐答案

React native无法为您提供侦听此类事件的方法,但是您有两种方法:一种可以部分工作,但非常简单,而另一种则可以.将按原样编写并按部就班地工作,但也需要更多的努力.

React native does not provide you with a way to listen for such events, but you have two approaches: one that will partially work but is extremely simple and one that will be written as it should and work as it should as well, but requires a lot more effort as well.

您可以使用setInterval创建一个计时器,该计时器将调用剪贴板. getString()(只记得它是异步的,所以您应该用await包装它或使用.then(...))并将其与从上一次调用中接收到的值进行比较.如果值不同,则用户复制了一些内容.如果您的应用程序在后台运行,则此方法将不起作用-为此,您应使用setInterval rel ="noreferrer">此库.此外,如果值相同,例如,它将不会捕获副本.如果用户先复制了文本"sample",然后再次进行复制,则不会检测到该文本,因为字符串是相同的.

You might create a timer with setInterval that would call Clipboard.getString() (just remember that it is async, so you should either wrap it with await or use .then(...)) and compare it with the value it received from the previous call. If the values differ, the user copied something. This method won't work if your app is in background - for that, you should substitute setInterval with a background service like this library. Moreover, it won't capture a copy if the value is the same, e.g. if the user first copied the text "sample" and then did it again, it won't detect it as the strings are the same.

您可能应该选择的解决方案是创建一个本机模块,该模块将分别实现iOS和Android的本机侦听器.在Android上,您可以绑定到 ClipboardManager OnPrimaryClipChangedListener,如下所示:

The solution you should probably choose is to create a native module that would implement a native listener for iOS and for Android, separately. On Android you can bind to the ClipboardManager's OnPrimaryClipChangedListener, like that:

void setupListener(){
    final ClipboardManager clipboardMgr = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);

    clipboardMgr.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
        public void onPrimaryClipChanged() {
            String contents = clipboardMgr.getText().toString();
            // do something with it, e.g. emit an event to JS
        }
    });
}

在iOS上,您可以使用 UIPasteboard UIPastedboardChangedNotification

And on iOS you can make use of UIPasteboard's UIPastedboardChangedNotification, like that:

func listener(sender: NSNotification){
    // do something
}

func setupListener(){
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("listener:"), name: UIPasteboardChangedNotification, object: nil)
}

这篇关于React Native-剪贴板中更改的侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆