8

I would like to get the text copied to clipboard when application launching.

I can use following text to get the available text from clipboard. But I need to use this value in a different viewcontroller. How can I pass this value to my viewcontroller?

- (void)applicationDidBecomeActive:(UIApplication *)application {

    NSLog([UIPasteboard generalPasteboard].string);

}
Rukshan
  • 7,902
  • 6
  • 43
  • 61
  • 2
    Why not just call `[UIPasteboard generalPasteboard].string` in your view controller? –  Aug 04 '12 at 13:01
  • viewController doesn't support applicationDidBecomeActive event. I want to get clipboard content when application become active from background. – Rukshan Aug 04 '12 at 13:10

3 Answers3

11

A much better way of handling this would be to add an observer (in the view controller) for the UIApplicationDidBecomeActiveNotification event. That way you avoid the unnecessary coupling between the app delegate and the view controller.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(getClipboardString:)
                                             name:UIApplicationDidBecomeActiveNotification object:nil];

Edit: Don't forgot to remove the observer when the view controller is removed:

[[NSNotificationCenter defaultCenter] removeObserver:self];
Arvid Janson
  • 1,034
  • 10
  • 10
9

Declare and implement a method on your VC which you call from the app delegate upon becoming active:

@inferface ViewController: UIViewController {
   /* etc. */
}

- (void)handlePasteboardString:(NSString *)s;

@end

in your app delegate:

- (void)applicationDidBecomeActive:(UIApplication *)a
{
    [self.mainViewController handlePasteboardString:[UIPasteboard generalPasteboard].string];
}
  • hmm i have tried that but it gives me an error : AppDelegate.m:65:12: Property 'ViewController' not found on object of type 'AppDelegate *' ... My main view is called ViewController, what is missing ? should i add some delegates to my viewcontroller ? – Fredv Jan 27 '13 at 09:02
0

Not sure when you want the clipboard item but if it's when that specific view controller is shown or about to be shown, you can do it in the viewDidLoad for that view controller

shim
  • 9,289
  • 12
  • 69
  • 108
apunn
  • 97
  • 8