clearing the general pasteboard?

Discussion in 'Mac Programming' started by binkley, Jun 5, 2007.

  1. binkley macrumors newbie

    Joined:
    Jun 5, 2007
    #1
    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?
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
  3. binkley thread starter macrumors newbie

    Joined:
    Jun 5, 2007
    #3
    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.
     
  4. lancestraz macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #4
    This works for me:
    Code:
    NSPasteboard *pb = [NSPasteboard generalPasteboard];
    [pb declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner: pb];
    [pb setString: @"" forType: NSStringPboardType];
     
  5. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    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.
     
  6. binkley thread starter macrumors newbie

    Joined:
    Jun 5, 2007
    #6
    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?
     
  7. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #7
    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:

    delegate NSApp to one of your files and then use,

    Code:
    -(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
    
     
  8. binkley thread starter macrumors newbie

    Joined:
    Jun 5, 2007
    #8
    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.
     
  9. binkley thread starter macrumors newbie

    Joined:
    Jun 5, 2007
    #9
    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.
     

Share This Page