Clipboard

How could I copy a text in the clipboard?

Jens

From memory…

dim c as new Clipboard
c.Text = "some text"
c.Close

Well, not sure that ClipBoard has been moved to iOS yet since that’s the old framework…

D’oh! I really need to pay attention to the topic.

[quote=158878:@Jens Holloch]How could I copy a text in the clipboard?
[/quote]

A declare will be necessary. The class that does this in iOS is UIPasteboard https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPasteboard_Class/index.html#//apple_ref/doc/uid/TP40008243

Fortunately, MacOSLib already contains declares to NSPasteboard and it looks possible to adapt the declare to UIKit. When you say “in the clipboard”, do you mean copy text from the clipboard, or to the clipboard ? Or both ?

Looking at MacOSLib, the methods used are not the same as what is needed in UIPasteboard. It does not seem out of this world, though. Somebody with more declare competence than mine will probably be able to do it fairly easily.

i want to copy a text from a Textfield to the clipboard.

As Michel said, you’ll need to use declares at the moment if you want to access the contents of the clipboard in code.

If you’re just asking how to do it generally, tap and hold your finger on the text in the TextField (or mouse pointer in the Simulator) and tap Select, then Copy. As Xojo uses native TextFields, you can copy text from one field and paste it (tap the blue cursor, select Paste) to another TextField or TextArea and it just works, no code required.

I used Jason King’s Declare Maker to make a UIPasteboard class. I believe the relevant method is :

Sub SetValueForPasteboardType(value as id, pasteboardType as CFStringRef) declare sub setValue_ lib "UIKit" selector "setValue:forPasteboardType:" (obj_id as ptr, value as ptr, pasteboardType as CFStringRef) setValue_(self, value, pasteboardType) End Sub

Now, how to call this method is the thousand dollars question. Is “self” the view handle or something else ? Is “Value” a CFStringRef or something else ?

The last item, “pasteboardtype”, should be an NSString, possibly a CFStringRef of content “String” if I understand right :

[code]The string value of the first pasteboard item.

Declaration
SWIFT
var string: String?
Discussion
The value stored in this property is an NSString object. The associated array of representation types is UIPasteboardTypeListString, which includes type kUTTypeUTF8PlainText. Setting this property replaces all current items in the pasteboard with the new item. If the first item has no value of the indicated type, nil is returned.[/code]

I tried this but it crashes. I am sure I am making a mistake I do not see, but it is dead simple for anybody familiar with the UIKit framework, though. It is a bit less easy to build a declare than to simply apply the recipe. I am trying to go beyond. Looking forward to “I wish I Knew How to Declare” from Eugene Dakin. I appreciate a fish now and then, but I would love to be able to fish for myself :wink:

Dim pasteboardType as CFStringRef = "String" Dim myMemoryBlock as MemoryBlock myMemoryBlock = TextEncoding.UTF8.ConvertTextToData("Hello Word", False) setValue(View1.Handle, myMemoryBlock.PtrValue, pasteboardType)

Self if going to be a reference to the UIPasteboard object. Also you should use stringWithString instead of passing a memory block like that. I’m not sure what the pasteboard type constant is going to be, but I know it won’t be “String” I can find that value later today. The Swift declaration there just means that it is of type string :slight_smile: So probably something like:

declare function generalPasteboard lib "UIKIt" selector "generalPasteboard" (clsRef as ptr) as ptr declare function NSClassFromString lib "Foundation" (clsName as cfstringref) as ptr declare function stringWithString lib "Foundation" selector "stringWithString:" (clsRef as ptr, str as cfstringref) as ptr setValue(generalPasteboard(NSClassFromString("UIPasteboard")), stringWithString("Hello world"), pasteboardType)

[quote=159020:@Jason King]Self if going to be a reference to the UIPasteboard object. Also you should use stringWithString instead of passing a memory block like that. I’m not sure what the pasteboard type constant is going to be, but I know it won’t be “String” I can find that value later today. The Swift declaration there just means that it is of type string :slight_smile: So probably something like:

declare function generalPasteboard lib "UIKIt" selector "generalPasteboard" (clsRef as ptr) as ptr declare function NSClassFromString lib "Foundation" (clsName as cfstringref) as ptr declare function stringWithString lib "Foundation" selector "stringWithString:" (clsRef as ptr, str as cfstringref) as ptr setValue(generalPasteboard(NSClassFromString("UIPasteboard")), stringWithString("Hello world"), pasteboardType)[/quote]

Thank you Jason. I knew it would be simple for someone in the know. I understand a little better. Thank you.

I looked through the class description in the iOS Library, but I cannot seem to understand what the method is expecting for pasteboardType :frowning: I will have to wait for your insight.

pasteboardType needs to be a string UTI. So if you are adding plain text to the pasteboard it will be “public.text”. Basically the UTI just has to match the type of object added to the pasteboard and it will work, otherwise nothing will be added to the pasteboard.
See
https://developer.apple.com/library/mac/documentation/MobileCoreServices/Reference/UTTypeRef/
for a list of available UTIs. You can resolve their values in a Swift playground with the following code:

import MobileCoreServices var tmp = kUTTypeText //change to each constant as declared in the docs above print(tmp)
And seeing what value is printed out. Hopefully this helps.

I get an error ;
Not enough arguments: got 1, expected at least 2. All what is bold is yellow in the error pane.

Oops, missed a param in stringWithString:

setValue(generalPasteboard(NSClassFromString("UIPasteboard")), stringWithString(NSClassFromString("NSString"),"Hello world"), pasteboardType)

You still need to resolve the proper pasteboard type.

Thanks for your help.

But I still need a little help. What is the proper pasteboard type for the text of a textfield and how I declare it?

Jens

I made a new method and took in it:

declare function generalPasteboard lib “UIKIt” selector “generalPasteboard” (clsRef as ptr) as ptr
declare function NSClassFromString lib “Foundation” (clsName as cfstringref) as ptr
declare function stringWithString lib “Foundation” selector “stringWithString:” (clsRef as ptr, str as cfstringref) as ptr
setValue(generalPasteboard(NSClassFromString(“UIPasteboard”)), stringWithString(NSClassFromString(“NSString”),“Hello world”), public.text)

But I got a syntax error for the last line.

Public.text needs to be quoted because it is a string. So it should be:

declare function generalPasteboard lib "UIKIt" selector "generalPasteboard" (clsRef as ptr) as ptr declare function NSClassFromString lib "Foundation" (clsName as cfstringref) as ptr declare function stringWithString lib "Foundation" selector "stringWithString:" (clsRef as ptr, str as cfstringref) as ptr setValue(generalPasteboard(NSClassFromString("UIPasteboard")), stringWithString(NSClassFromString("NSString"),"Hello world"), "public.text")

I tried it, but I got “This item does not exists” for the setValue command.

You need to add the declare from Michel’s post above:

declare sub setValue lib "UIKit" selector "setValue:forPasteboardType:" (obj_id as ptr, value as ptr, pasteboardType as CFStringRef)