0

I want hook the propery string in UIPasteboard, when it changes, i will do somethings. the property defined as:

@property(nullable,nonatomic,copy) NSString *string

I use method swizzle replace string to stringSwizzle, setString to setStringSwizzle like follows:

- (void) setStringSwizzle:(NSString *) string {
    NSLog(@"setStringSwizzle: %@", string);
    [self setStringSwizzle:string];
}

- (NSString *) stringSwizzle {
    NSLog(@"stringSwizzle - %@", [self stringSwizzle]);
    return [self stringSwizzle];
}

When i use code as as follows, it will enter setStringSwizzle and output my log:

UIPasteboard *uiPasteboard = [UIPasteboard generalPasteboard];
uiPasteboard.string = @"test1";
[uiPasteboard setString:@"test2"];
[uiPasteboard setValue:@"test3" forKey:@"string"];

However, when i do a copy action in UITextView, it will not output any setStringSwizzle log(but the method stringSwizzle will out put log).

So, it there any way to achieve my goal?

-------------

plus: i use KVO to observing the vale string of UIPasteboard, and when i do copy action in UITextview, the observer will not response.

waitianlou
  • 563
  • 3
  • 15

1 Answers1

0

To Copy :

 UIPasteboard *pb = [UIPasteboard generalPasteboard];
    [pb setString:[textView text]];

To Paste :

UIPasteboard *pb = [UIPasteboard generalPasteboard];
    textView.text = [pb string];
Munahil
  • 2,381
  • 1
  • 14
  • 24