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

时间:2013-08-12 03:41:25

标签: javascript ios objective-c cordova phonegap-plugins

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

我遇到过这个插件

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

但是这个似乎有点过时了。

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

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>

但是在尝试使用

将文本复制到cliboard时

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

事情没有成功。如何让这个插件运行。谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

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