Swift3.0 clipboard code copy and cross application paste implementation code

  • 2020-05-24 06:17:54
  • OfStack

There is a requirement that you click a button to copy 1 piece of content, and then go to another application and press paste directly.

The implementation is as follows:


override func viewDidLoad() {
 super.viewDidLoad()

 // Do any additional setup after loading the view.
 view.backgroundColor = UIColor.white
 
 // Paste in the application, this one txt For testing 
 let txt = UITextField(frame: CGRect(x: 100, y: 100, width: 100, height: 33))
 txt.backgroundColor = UIColor.yellow
 txt.placeholder = "shuru"
 view.addSubview(txt)

 
 //1. Test shears 
 testPasteBoard(str: "123")
 
 
 }

 
 ///  Test the clipboard to copy the content of the code 
 ///  Conclusion: long press the input box in the application can be realized after pasting, the application can still be outside the application. 
 func testPasteBoard(str:String) {
 
 // Those are the two sentences 
 let paste = UIPasteboard.general
 paste.string = str
 
 
 } 

After copying, you can paste both inside and outside your current application.


Related articles: