類(lèi)似于淘口令的應(yīng)用,打開(kāi)app檢測(cè)粘貼板內(nèi)容,但是要求不能更改剪切板的內(nèi)容珊皿,在自己的應(yīng)用內(nèi)只能檢測(cè)一次,再者如果是自己應(yīng)用內(nèi)的淘口令巨税,自己不能檢測(cè)
插件鏈接:https://github.com/MonkZL/react-native-clipboard-command
插件用法
import { setCommand, useCommand } from 'react-native-clipboard-command';
export default function App() {
const [commandStr, setCommandStr] = React.useState('');
useCommand(
(command) => {
alert('復(fù)制過(guò)來(lái)的指令是: ' + command);
},
(reason) => {
alert(reason);
}
);
return (
<View style={styles.container}>
<TextInput
placeholder={'請(qǐng)輸入口令'}
value={commandStr}
onChangeText={setCommandStr}
style={{
width: '50%',
height: 50,
borderWidth: 1,
paddingHorizontal: 10,
}}
/>
<TouchableOpacity
style={{
width: 100,
height: 100,
backgroundColor: 'red',
alignItems: 'center',
justifyContent: 'center',
}}
onPress={() => {
setCommand(commandStr);
alert('設(shè)置成功');
}}
>
<Text>設(shè)置口令</Text>
</TouchableOpacity>
</View>
);
}
android的插件實(shí)現(xiàn)方式 通過(guò)給ClipData
設(shè)置lable來(lái)判斷是否是自己復(fù)制到粘貼板的內(nèi)容
@ReactMethod
public void setCommand(String command) {
try {
ClipData clipdata = ClipData.newPlainText(getPackageName(), command);
ClipboardManager clipboard = getClipboardService();
clipboard.setPrimaryClip(clipdata);
} catch (Exception e) {
e.printStackTrace();
}
}
@ReactMethod
public void getCommand(Promise promise) {
try {
ClipboardManager clipboard = getClipboardService();
ClipData clipData = clipboard.getPrimaryClip();
if (clipData == null) {
promise.reject("0", "沒(méi)有數(shù)據(jù)");
return;
}
CharSequence label = clipData.getDescription().getLabel();
if (TextUtils.equals(label, getPackageName())) {
promise.reject("1", "自己在應(yīng)用內(nèi)復(fù)制的指令");
return;
}
if (clipData.getItemCount() >= 1) {
ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0);
promise.resolve("" + firstItem.getText());
} else {
promise.reject("0", "沒(méi)有數(shù)據(jù)");
}
} catch (Exception e) {
promise.reject("2", e.getMessage());
}
}
ios的插件實(shí)現(xiàn)方式 通過(guò)[systemBoard addItems:@[item]]
的方式設(shè)置標(biāo)記
RCT_EXPORT_METHOD(setCommand:(NSString *)command)
{
//創(chuàng)建系統(tǒng)剪切板
UIPasteboard *systemBoard = [UIPasteboard generalPasteboard];
//將文本寫(xiě)入剪切板
systemBoard.string = command;
//給剪切板加入一條標(biāo)記性的數(shù)據(jù)蟋定,只是為了檢測(cè)剪切板的數(shù)據(jù)是否來(lái)自當(dāng)前應(yīng)用
NSDictionary<NSString *, id> *item = @{[[NSBundle mainBundle]bundleIdentifier]:command};
[systemBoard addItems:@[item]];
}
RCT_EXPORT_METHOD(getCommand:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject)
{
@try {
//創(chuàng)建系統(tǒng)剪切板
UIPasteboard *systemBoard = [UIPasteboard generalPasteboard];
if(!systemBoard.numberOfItems) {
reject(@"0",@"沒(méi)有數(shù)據(jù)", nil);
return;
}
NSArray<NSDictionary<NSString *, id> *> *items = systemBoard.items;
long count = items.count;
for(int i=0; i < count; i++){
NSDictionary<NSString *, id> *item = [items objectAtIndex:i];
if([[item allKeys] containsObject:[[NSBundle mainBundle]bundleIdentifier]]){
reject(@"1",@"自己在應(yīng)用內(nèi)復(fù)制的指令", nil);
return;
}
}
resolve((systemBoard.string ? : @""));
} @catch (NSException *exception) {
reject(@"2",exception.reason, nil);
} @finally {
}
}