Tip: Copy and Paste with The Pasteboard

iOS Development tips weekly is a series you can find at the Lynda.com and LinkedIn Learning libraries. The first week of a week’s tip will be available to the public. After that, you will need a subscription to get access to it. Click the image the left  to view.  You’ll find a transcript below.

All day long you use Command-X, Command-C and Command-V to cut, copy and paste from the pasteboard. You see that functionality in other applications. How do you get that functionality in an iOS application? In this tip, I’ll show you the basics of using the pasteboard to copy and paste data not only in your app, but over the sandbox wall and into other applications.

If you run the starter file, I have set up a small application to type into the textview. There’s two buttons on the top for copying and pasting the text.

Stop the app and we’ll make the code for those buttons.

Before we do, I’ll add one constant.

let pasteboard = UIPasteboard.general

The general pasteboard is the only persistent pasteboard, and the only pasteboard which can copy and paste outside your app.

Move down the code a bit to the copyText action. Many of the simple types like strings and numbers have special methods and properties to quickly copy to the pasteboard. So the simplest way to copy text is this:

pasteboard.string = typingText.text

I’ll actually make this more of a cut than a copy, and delete the text after I’ve copied it.

typingText.text = ""

Taking the text off the pasteboard is just as easy. In the pasteText action,  assingn the pasteboard text to the I.

typingText.text = pasteboard.string

Run this on a simulator.  I’ll quickly type

I love a good pizza!!

 

then copy it.  I’ll paste it back right after. And it appears.

The general pasteboard has one ability othere pasteboards don’t – you can copy and paste between apps. This behavior does not work the simulator though, only a real device. I’ll stop the app in the simulator. I’ve plugged in my iPad mini and I’ll run it there.

This time, I”ll jump out if the app and to notes, where I have something already written, a selection for an article I wrote on LinkedIn Pulse.

I’ll select and copy it, and back in minityper hit paste.

I used cut to clear the screen, type something out

I like Pizza

and paste it back to notes

Stop the app. You also can cut and paste images, using the image property. Of course you’ll have to know what is an image and what is a string.  To do that, we’ll check if the clipboard is a string or a image.  I’ll add this as the first line of the pasteTet action:

if pasteboard.hasStrings{

I’ll put the closing bracket after the line already entered.  the hasStrings quickly checks the pasteboard for strings. If there, it will paste the string.  Under that I’ll add another if clause to check for images.

if pasteboard.hasImages{

backgroundImage.image = pasteboard.image

}

The code checks for an image on the pasteboard. If an image, the app pastes it to the background image view I set up.

Run this.  I’ll go to the photos, where I am in my food photography album, select a photo and then copy it.

 

I’ll then paste it in minityper. The photo appears.

That’s the basics with some of the simplest ways of copying and pasting using the clipboard. There’s more to explore here, including other objects such as color and data,  multiple objects on the pasteboard and your own internal pasteboards. Check the apple documentation for UIPasteboardfor more details.

The Whole Code

Below you will find the code for this project. For the full downloadable project check out the repository on GitHub

ViewController.swift

//
//  ViewController.swift
//  MiniTyper
//
//  Created by Steven Lipton on 11/27/17.
//  Copyright © 2017 Steven Lipton. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var typingText: UITextView!
    @IBOutlet weak var backgroundImage: UIImageView!
    let pasteboard = UIPasteboard.general
    
    @IBAction func copyText(_ sender: UIButton) {
        pasteboard.string = typingText.text
        typingText.text = ""
    }
    
    @IBAction func pasteText(_ sender: UIButton) {
        if pasteboard.hasStrings{
            typingText.text = pasteboard.string
        }
        if pasteboard.hasImages{
            backgroundImage.image = pasteboard.image
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        typingText.becomeFirstResponder()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.