RahulSalvikar
RahulSalvikar

Reputation: 658

How to nill the value of UIPasteboard

1) I copy image from UIPasteboard using this method [UIPasteboard generalPasteboard].image;

2) After creating an image i want to clean the UIPasteboard. so i write this method [UIPasteboard generalPasteboard].image = nil; But this working fine on ios 4 and ios 5 But it gives problem on ios 6.

3) In my app i want to clean UIPasteboard or nil the UIPasteboard value. How can i do this on ios 6?

Upvotes: 0

Views: 1781

Answers (2)

Buzzy
Buzzy

Reputation: 3677

I am sorry but the accepted answer is just misleading/incorrect. This will not clear the existing value from the pasteboard.

The second argument to setValue:forPasteboardType: is a string identifier for the item. As the documentation suggests, it may be a UTI (look in MobileCoreServices) or a user defined string.

The method call in the accepted answer uses UIPasteboardNameGeneral which is an identifier for the entire pasteboard itself (and not a key in one if the items in the array of dictionaries in [UIPasteboard items]). Since the pasteboard does not contain an item for that key, that call will be a no-op. It is easy to verify this yourself. [[UIPasteboard generalPasteboard] name] is equal to UIPasteboardNameGeneral. Also, containsPasteboardTypes: using that key will return NO.

Now to address what might be happening in iOS 6. Apple is likely using a UTI (say kUTTypePNG) as the key for the image data being stored in the pasteboard. They are likely ensuring that the value being set for that key is indeed an NSData (which nil is not).

To address your conundrum, may I suggest one of the following:

  • If you don't want to share the contents that you put onto the clipboard with other applications, you can create an application specific clipboard using + (UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create
    • The contents of this clipboard will only be accessible to your application and will be deleted when your application is deleted.
    • The contents of the clipboard will be cleared when the app quits unless you set the persistent property to YES on the same.
    • You can still access items placed onto the clipboard by other applications by accessing the general clipboard.
  • If you absolutely must clear the contents (given the limitations listed), you can try setting another valid image. I have doubts about this approach since there may be multiple keys for a given clipboard item.

Hope this helps.

Upvotes: 1

nizx
nizx

Reputation: 690

This is what I currently use

UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setValue:@"" forPasteboardType:UIPasteboardNameGeneral];

Upvotes: 0

Related Questions