使用 Phonegap 复制到剪贴板剪贴板 iOS

Posted

技术标签:

【中文标题】使用 Phonegap 复制到剪贴板剪贴板 iOS【英文标题】:Copy to clipboard clipboard iOS using Phonegap 【发布时间】:2013-08-12 03:41:25 【问题描述】:

我正在使用 cordova 2.5.0。我正在尝试将文本复制到 ios 中的剪贴板。

我遇到了这个插件

https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/ClipboardPlugin

但是这个好像有点过时了。

我已将 ClipboardPlugin.hClipboardPlugin.m 放在 Plugins 文件夹中,并根据最新版本修改了类。

ClipboardPlugin.h

//
//  ClipboardPlugin.h
//  Clipboard plugin for PhoneGap
//
//  Copyright 2010 Michel Weimerskirch.
//

#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>

@interface ClipboardPlugin : CDVPlugin 

-(void)setText:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

-(void)getText:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

@end

ClipboardPlugin.m

//
//  ClipboardPlugin.m
//  Clipboard plugin for PhoneGap
//
//  Copyright 2010 Michel Weimerskirch.
//

#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h>
#import "ClipboardPlugin.h"

@implementation ClipboardPlugin

-(void)setText:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options 
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSString     *text       = [arguments objectAtIndex:0];

    [pasteboard setValue:text forPasteboardType:@"public.text"];


-(void)getText:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options 
    NSString* callbackID = [arguments pop];
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

    NSString *text = [pasteboard valueForPasteboardType:@"public.text"];

    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:text];

    [self writejavascript: [pluginResult toSuccessCallbackString:callbackID]];


@end

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
-->
<widget>
    <preference name="KeyboardDisplayRequiresUserAction" value="true" />
    <preference name="SuppressesIncrementalRendering" value="false" />
    <preference name="UIWebViewBounce" value="true" />
    <preference name="TopActivityIndicator" value="gray" />
    <preference name="EnableLocation" value="false" />
    <preference name="EnableViewportScale" value="false" />
    <preference name="AutoHideSplashScreen" value="true" />
    <preference name="ShowSplashScreenSpinner" value="true" />
    <preference name="FadeSplashScreen" value="true" />
    <preference name="FadeSplashScreenDuration" value=".25" />
    <preference name="MediaPlaybackRequiresUserAction" value="false" />
    <preference name="AllowInlineMediaPlayback" value="false" />
    <preference name="BackupWebStorage" value="cloud" />

    <content src="initScreen.html" />

    <plugins>
        <plugin name="Device" value="CDVDevice" />
        <plugin name="Logger" value="CDVLogger" />
        <plugin name="Compass" value="CDVLocation" />
        <plugin name="Accelerometer" value="CDVAccelerometer" />
        <plugin name="Camera" value="CDVCamera" />
        <plugin name="NetworkStatus" value="CDVConnection" />
        <plugin name="Contacts" value="CDVContacts" />
        <plugin name="Debug Console" value="CDVDebugConsole" />
        <plugin name="Echo" value="CDVEcho" />
        <plugin name="File" value="CDVFile" />
        <plugin name="FileTransfer" value="CDVFileTransfer" />
        <plugin name="Geolocation" value="CDVLocation" />
        <plugin name="Notification" value="CDVNotification" />
        <plugin name="Media" value="CDVSound" />
        <plugin name="Capture" value="CDVCapture" />
        <plugin name="SplashScreen" value="CDVSplashScreen" />
        <plugin name="Battery" value="CDVBattery" />
        <plugin name="Globalization" value="CDVGlobalization" />
        <plugin name="InAppBrowser" value="CDVInAppBrowser" />
        <plugin name="ClipboardPlugin" value="ClipboardPlugin" />
    </plugins>
    <access origin="*" />
</widget>

但是当尝试使用

将文本复制到剪贴板时

window.plugins.clipboardPlugin.setText("omg\n this rules")

事情没有解决。我怎样才能让这个插件运行。谢谢。

【问题讨论】:

【参考方案1】:

您的要求是将文本复制到 ios 中的剪贴板,使用此代码:

    [UIPasteboard generalPasteboard].string = @"text you want to copy";

或使用 UIPasteboard 类来完成您的要求。

【讨论】:

OP 可能想在 PhoneGapp“HTML5 应用程序”中通过 JavaScript 来实现。 (我也想这样做)

以上是关于使用 Phonegap 复制到剪贴板剪贴板 iOS的主要内容,如果未能解决你的问题,请参考以下文章

使用 iOS 将文本复制到剪贴板

点击复制文字到剪贴板兼容性安卓ios

支付宝红包口令自动复制到剪贴板脚本js,安卓,IOS通用版

如何使用 Swift 将文本复制到剪贴板/粘贴板

IOS剪贴板(复制粘贴)不能跨应用程序

复制内容到剪贴板