Omer Abbas
Omer Abbas

Reputation: 205

Why doesn't my UIPasteboard code work?

I am having a weird issue with the UIPasteboard.

I am copying text from Safari, and then in my app looking into UIPasteboard to see if it contains any data using this code:

[[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObject:@"public.utf8-plain-text"]]

It's working fine with the Simulator but not working with the iPad. Is this because of any character set issue?

Upvotes: 2

Views: 3928

Answers (2)

Omer Abbas
Omer Abbas

Reputation: 205

I was having this issue because of character set but i am not sure it was only with the IOS 5.0 or for all version. But i have solved my problem with a simple solution,

UIPasteboardTypeListString

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIPasteboard_Class/Reference.html

 [[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListString];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; 
if (pasteboard.string != nil) { [self insertText:pasteboard.string]; }

I hope this answer can help anyone.

Upvotes: 4

appmattus
appmattus

Reputation: 2808

From my own experiences it appears that in iOS 5 plain text no longer ends up on the pasteboard as public.utf8-plain-text, but instead as public.text. Using UIPasteboardTypeListString instead of explicitly specifying the string will also work.

So I now use the following in my code to detect plain text in the pasteboard:

[[UIPasteboard generalPasteboard] containsPasteboardTypes:[NSArray arrayWithObjects:@"public.utf8-plain-text", @"public.text", nil]]

or

[[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListString]

Is it the case you are testing in the simulator against iOS 4.x and your iPad has iOS 5 installed?

Upvotes: 1

Related Questions