0

I'm writing a keyboard APP , I want to get the text in pasteboard , so i use UIPasteboard and this function [UIPasteboard generalPasteboard].

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPasteboard *paste = [UIPasteboard generalPasteboard];

self.outputLabel.text = paste.string;

it works , I can get the string and i know the string i paste is on pasteboard . So i want get the string in paste real-time,i use observe and

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPasteboard *paste = [UIPasteboard generalPasteboard];
    self.paste = paste;

    self.outputLabel.text = paste.string;

    [paste addObserver:self forKeyPath:@"key" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

    self.outputLabel.text = self.paste.string;
}

but it doesn't work , even the function doesn't run .

Can anybody help me?

Also if anybody have written APP like keyboard , you can send email to me , let's discuss the bug in keyboard :) .My email is yuyanganhui@outlook.com !

vaibhav
  • 4,038
  • 1
  • 21
  • 51
好多余先生丶
  • 261
  • 1
  • 2
  • 9
  • Are you looking for `UIPasteboardChanged`? Change seems to be post a `NSNotification` management. – Larme Sep 20 '16 at 09:35
  • thanks for your comment , i had try . should i use NSNotification defaultCenter ? like this `NSNotification *noti = [NSNotification notificationWithName:@"UIPasteboardChanged" object:nil userInfo:nil]; [[NSNotificationCenter defaultCenter] postNotification:noti]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChange) name:@"UIPasteboardChanged" object:nil];`but i don't know where i have make wrong then it dose not work – 好多余先生丶 Sep 20 '16 at 10:07

0 Answers0