215

What is the best way to copy text to the iPhone's clipboard in your application?

Their docs are sketchy and have way more features than what I want... I just want to set a string as the users clipboard.

3
  • 19
    it's been 7 years and you still haven't accepted the correct answer below Sep 26, 2016 at 3:35
  • Can't a mod accept the answer on his behalf?
    – GrayedFox
    Dec 15, 2016 at 14:32
  • Please accept an answer for this question.
    – CristiC
    Sep 12, 2017 at 9:00

2 Answers 2

634

Although the accepted answer is a good walkthrough of how UIPasteboard works, I figured I'd post the relevant snippet right here for everyone's convenience:

Obj-C

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"Paste me!";

Swift 2.2

let pasteBoard = UIPasteboard.generalPasteboard()
pasteBoard.string = "Paste me!"

Swift 3+:

let pasteBoard = UIPasteboard.general
pasteBoard.string = "Paste me!"
3
  • 6
    Thanks for that, much more helpful than sifting through a big tutorial.
    – Chris
    Feb 26, 2012 at 0:35
  • 14
    Yep, thanks for this. This is IMO how a question should be answered.
    – GeneCode
    Apr 5, 2012 at 9:42
  • 3
    Even in 2017 this is still the accepted answer to be.
    – shinyuX
    Jan 17, 2017 at 17:12
35

Swift 2.2 :

let pasteBoard = UIPasteboard.generalPasteboard()
pasteBoard.string = "Paste Me !"

Swift 3:

let pasteBoard = UIPasteboard.general
pasteBoard.string = "Paste me!"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.