1

我正在使用科尔多瓦 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")

事情不顺利。我怎样才能让这个插件运行。谢谢。

4

1 回答 1

0

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

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

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

于 2013-08-12T08:17:32.483 回答