Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic iPhone pasteboard usage

I'm trying to put some plain text in the iPhone Pasteboard. The following code doesn't seem to work:

UIPasteboard *pboard = [UIPasteboard generalPasteboard];
NSString *value = @"test";
[pboard setValue: value forPasteboardType: @"public.plain-text"];

I'm guessing the problem is in the PasteBoard type argument. Passing @"public.plain-text" nothing happens. Passing kUTTypePlainText the compiler complains about incompatible pointer type, but doesn't crash, and nothing happens either. Using kUTTypePlainText also seems to require linking with MobileCoreServices, which is not mentioned in the docs.

like image 351
Luís Marques Avatar asked Jun 28 '09 18:06

Luís Marques


People also ask

What is a pasteboard on iPhone?

An object that helps a user share data from one place to another within your app, and from your app to other apps. iOS 3.0+ iPadOS 3.0+ Mac Catalyst 13.1+

What is clipboard data on iPhone?

You can use Universal Clipboard to cut or copy content (a block of text or an image, for example) on your iPhone, then paste it on iPad, on another iOS device, or Mac computer, and vice versa.

How does Apple clipboard work?

Each Mac requires macOS High Sierra or later. The content is automatically added to the clipboard of your other nearby device. It remains there briefly, or until you replace it by copying something else on either device. On the other device, paste the content as you normally would.


3 Answers

Use this header to get the value for kUTTypeUTF8PlainText;

#import <MobileCoreServices/UTCoreTypes.h>

You'll need to have the MobileCoreServices framework available.

like image 177
kickingvegas Avatar answered Oct 19 '22 19:10

kickingvegas


Responding to the comments and my own question:

  • Setting the pasteboard string property works.
  • Using setValue:forPasteboardType: also works if I use kUTTypeUTF8PlainText instead of kUTTypePlainText for the pasteboard type.

I had not noticed the string property because I went directly to the "Getting and Setting Single Pasteboard Items" tasks section.

The way I was testing was by clicking in a text field and see if the paste pop-up would appear.

I still am not sure where in the docs the UTT types are explained for the iPhone, including where to get them (Framework, #include files), it seems that the "Uniform Type Identifiers Overview" doc is still geared toward Mac OS. Since the constants gave me a type mismatch warning I thought I was doing something wrong, that's why I first tried using an NSString literal.

like image 44
Luís Marques Avatar answered Oct 19 '22 19:10

Luís Marques


Here's my experiments with pasting text onto the pasteboard. I'm using a button to add the text programatically.

#import <MobileCoreServices/MobileCoreServices.h>

- (IBAction)setPasteboardText:(id)sender
{
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    NSString *text = @"東京京都大阪";

    // Works, but generates an incompatible pointer warning
    [pb setValue:text forPasteboardType:kUTTypeText];

    // Puts generic item (not text type), can't be pasted into a text field
    [pb setValue:text forPasteboardType:(NSString *)kUTTypeItem];

    // Works, even with non-ASCII text
    // I would say this is the best way to do it with unknown text
    [pb setValue:text forPasteboardType:(NSString *)kUTTypeText];

    // Works without warning
    // This would be my preferred method with UTF-8 text
    [pb setValue:text forPasteboardType:(NSString *)kUTTypeUTF8PlainText];

    // Works without warning, even with Japanese characters
    [pb setValue:text forPasteboardType:@"public.plain-text"];

    // Works without warning, even with Japanese characters
    [pb setValue:text forPasteboardType:@"public.text"];

    // Check contents and content type of pasteboard
    NSLog(@"%@", [pb items]);
}

I pasted the contents into a text field to check, and changed the text contents each time to make sure it wasn't just re-using the previous paste.

like image 3
nevan king Avatar answered Oct 19 '22 21:10

nevan king