views:

1744

answers:

3

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.

+1  A: 

Have you tried @"public.text"?

Diederik Hoogenboom
Yes, it also did nothing.
Luís Marques
Please don't ever type in UTI strings unless you need to. It's just asking for trouble, and there's a reason Apple provides constants for them all. You just need to cast to an NSString before using them.
Mike Abdullah
+3  A: 

Responding to the comments and my own question:

  • Setting the pastboard's 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.

Luís Marques
It's got nothing to do with being geared towards Mac OS X, as I explained, you just need to cast the string. The constants are declared as CFStringRef, which is tollfree bridged with NSString. Just do: (NSString *)kUTTypePlainText
Mike Abdullah
+6  A: 

Use this header to get the value for kUTTypeUTF8PlainText;

#import <MobileCoreServices/UTCoreTypes.h>

You'll need to have the MobileCoreServices framework available.

kickingvegas