phonegap(cordova) 入门 14---- 复制粘贴功能

来源:互联网 发布:钢铁雄心4多核优化补丁 编辑:程序博客网 时间:2024/06/07 19:45

大家都知道,js 是没办法直接操作本机剪切板的,那走插件呗,如下

android 

public class ClipboardPlugin extends CordovaPlugin {private static final String actionCopy = "copy";    private static final String actionPaste = "paste";@Overridepublic boolean execute(String action, JSONArray args,CallbackContext callbackContext) throws JSONException { ClipboardManager clipboard = (ClipboardManager) cordova.getActivity().getSystemService(Context.CLIPBOARD_SERVICE);if (action.equals(actionCopy)) {            try {                String text = args.getString(0);                ClipData clip = ClipData.newPlainText("Text", text);                clipboard.setPrimaryClip(clip);                callbackContext.success(text);                return true;            } catch (JSONException e) {            return false;            } catch (Exception e) {            return false;            }        } else if (action.equals(actionPaste)) {            if (!clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {            return false;            }            try {                ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);                String text = item.getText().toString();                if (text == null) text = "";                callbackContext.success(text);                return true;            } catch (Exception e) {            return false;            }        }        return false;} }
iOS

#import "CDVClipboard.h"@implementation CDVClipboard@synthesize callbackID;- (void)copy:(CDVInvokedUrlCommand*)command {    [self.commandDelegate runInBackground:^{        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];        NSString     *text       = [command.arguments objectAtIndex:0];                [pasteboard setValue:text forPasteboardType:@"public.text"];                CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];    }];}- (void)paste:(CDVInvokedUrlCommand*)command {    [self.commandDelegate runInBackground:^{        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];        NSString     *text       = [pasteboard valueForPasteboardType:@"public.text"];                CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];    }];}@end



0 0
原创粉丝点击