Getting text From Clipboard or Pasteboard

Using the following to try and get text from system clipboard

// Get a reference to the system pasteboard
Declare Function NSClassFromString Lib “Foundation” (className As CFStringRef) As Ptr
Var uiPasteboardClass As Ptr = NSClassFromString(“UIPasteboard”)
Declare Function generalPasteboard Lib “UIKit” Selector “generalPasteboard” (classRef As Ptr) As Ptr
Var pasteboard As Ptr = generalPasteboard(uiPasteboardClass)

// Get the value from the pasteboard
Declare Sub getValue Lib “UIKit” Selector “ValueForPasteboardType:” (pasteboardReference As Ptr, _pasteboardType As CFStringRef) as CFStringRef

var cliptext As String
cliptext = getValue(generalPasteboard(NSClassFromString(“UIPasteboard”)), “public.text”)

return cliptext

Missing something getting Syntax error Declare Sub get value any thoughts?

Can you not just use the Clipboard class?

Var c As Clipboard

// create an instance of the clipboard
c = New Clipboard

// check for textAvailable
If c.TextAvailable Then
  // text is available, set the TextField to it
  TextField1.Text = c.Text
End If

// close it
c.Close
2 Likes

Yes good point it seems to work. That’s much easier.

Although I am getting an error on c.Close

“Clipboard” has no member named Close

I just put the following in the pressed method of a button and it worked fine:

Var c As New Clipboard
Var s As String = c.Text
MessageBox s
c.Close

What version of Xojo are you using.

Interesting

When I type in c. and hit tab to display items that can be used “Close” is not there?

everything seems to be working other than that.

Screen Shot 2022-04-21 at 6.10.08 AM

2022 R1

What is your project type? Mine is desktop, but that shouldn’t matter.

Screenshot 2022-04-21 at 11.33.06

Another thought. You haven’t created a clipboard class yourself have you?

1 Like

ios

Look at which section of the forum this thread is in :wink:

And, well, it actually matters; I’m seeing the same thing.
Clipboard support for iOS isn’t extensive yet. For the time being, you can just let the reference go out of scope (e.g. setting the variable to nil, or exiting the current sub/function) and this should release the clipboard.

1 Like

I thought I had hidden that forum. I did end up reading the forum without being logged in at one point. Likely the source of my confusion.

Just to be complete do not have a global variable / app property and keep a copy of the clipboard object in there. It would lock the clipboard open.

1 Like

I added this method to a Module, and it gets the text from the iOS clipboard for me.

Public Function GetClipboardText() As String
  Declare Function generalPasteboard Lib "UIKIt" Selector "generalPasteboard" (clsRef As ptr) As ptr
  Declare Function NSClassFromString Lib "Foundation" (clsName As cfstringref) As ptr
  Declare Function getValue Lib "UIKit" Selector "valueForPasteboardType:" (obj_id As ptr, pasteboardType As CFStringRef) As CFStringRef
  
  
  Var myString As String = getValue(generalPasteboard(NSClassFromString("UIPasteboard")), "public.text")
  
  Return myString
End Function

Thank you sir!