iOS SDK getting clipboard text on application load

Rukshan Marapana picture Rukshan Marapana · Aug 4, 2012 · Viewed 6.9k times · Source

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);

}

Answer

Arvid Janson picture Arvid Janson · Jul 28, 2013

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];