Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

binkley

macrumors newbie
Original poster
Jun 5, 2007
5
0
I've been searching for a way to ensure that my objective-c/cocoa application can clear the general pasteboard when it exits. So far every tutorial/doc that I've found will tell you how to add data to the pasteboard, but not delete it.

For example, I have the following code in my app:

Code:
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner: self];
[pb setString: @"foo" forType: NSStringPboardType];

I've tried using the same code in my AppController's dealloc routine using the string @"", but I can still paste "foo" well after the application exits. Any ideas?
 

binkley

macrumors newbie
Original poster
Jun 5, 2007
5
0
Why do you want to do this?

The user will be expected to copy some security sensitive information to the pasteboard. In addition to wanting this information to be removed upon exit, I'll also be setting up a timer to delete it after a set period of time.
 

lancestraz

macrumors 6502a
Nov 27, 2005
898
0
RI
This works for me:
Code:
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner: pb];
[pb setString: @"" forType: NSStringPboardType];
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Its a security risk copying it to the pasteboard at all as someone could very easily make an application run in the background and get the sensitive information.

I would try another technique (a button in the interface and writing your own code to "copy" the information) if possible.

To solve your problem you could just copy a random string (or number) to the pasteboard to "clear" it, but you need to check the user hasn't copied anything else since otherwise that would be really really irritating.
 

binkley

macrumors newbie
Original poster
Jun 5, 2007
5
0
Aside from the wisdom of using this technique.. maybe my dealloc routine is never being called? Where would the best place be to put code to make sure the pasteboard is cleared when the program exits?
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Aside from the wisdom of using this technique..
I regularly cut items to the pasteboard for later use. If I was using an application which then went and overwrote that I wouldn't be happy, and I simply wouldn't use the application (i'd probably give it a 1* review too).

If you can store the data programatically that would be a far better solution.

But with that caveat:

Where would the best place be to put code to make sure the pasteboard is cleared when the program exits?

delegate NSApp to one of your files and then use,

Code:
-(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
 

binkley

macrumors newbie
Original poster
Jun 5, 2007
5
0
I appreciate your help. Believe me, the users of this application would expect this behavior. They'd be far more concerned if the pasteboard was left intact rather than deleted unexpectedly.
 

binkley

macrumors newbie
Original poster
Jun 5, 2007
5
0
I set up my AppController as a delegate and implemented the following routine and everything works fine:

Code:
- (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) sender 
{

  NSPasteboard *pb = [NSPasteboard generalPasteboard];
  [pb declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner: self];
  [pb setString: @"" forType: NSStringPboardType];

  return NSTerminateNow;
}

Thanks again.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.