How to paste programmatically using Swift?

I want a button to press a button that automatically pastes any text into the clipboard UITextView.

How can I do this in Swift?

An answer was given here, but it is in Objective-C.

+4
source share
2 answers

You can simply convert them to Swift:

@IBAction func copy() {
    let pb: UIPasteboard = UIPasteboard.generalPasteboard();
    pb.string = textView.text // Or another source of text
}

@IBAction func paste() {
    let pb: UIPasteboard = UIPasteboard.generalPasteboard();
    textView.text /*(Or somewhere to put the text)*/ = pb.string
}
+10
source

You can implement the copy and paste method in Swift, for example:

// Function receives the text as argument for copying
func copyText(textToCopy : NSString)
{
    let pasteBoard    = UIPasteboard.generalPasteboard();
    pasteBoard.string = textToCopy; // Set your text here
}

// Function returns the copied string
func pasteText() -> NSString
{
    let pasteBoard    = UIPasteboard.generalPasteboard();
    println("Copied Text : \(pasteBoard.string)"); // It prints the copied text
    return pasteBoard.string!;
}
+4
source

Source: https://habr.com/ru/post/1569100/


All Articles