214

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
tarnfeld
  • 25,992
  • 41
  • 111
  • 146

2 Answers2

633

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!"
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
samvermette
  • 40,269
  • 27
  • 112
  • 144
35

Swift 2.2 :

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

Swift 3:

let pasteBoard = UIPasteboard.general
pasteBoard.string = "Paste me!"
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Mojtaba Yeganeh
  • 2,788
  • 1
  • 30
  • 49