iOS13 UIPasteboard.general.changeCount increase 2 after UITextfield become first responder

from iOS13, UIPasteboard.general.changeCount increase 2 after UITextfield become first responder in my demo project that just contain a viewcontroller with a UITextfield. but this just happen in my project, but system application haven't this problem. how can i solve this problem?


the demo code as following:


@interface ViewController ()

@property (nonatomic, strong) UITextField *text;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNoti:) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNoti:) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNoti:) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNoti:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNoti:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNoti:) name:UIPasteboardChangedNotification object:nil];

    _text = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
    _text.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_text];
    _text.center = [self.view center];
    _text.delegate = self;
    
    self.view.backgroundColor = [UIColor lightGrayColor];

}

-(void)didReceiveNoti: (NSNotification *)noti{
    if (_text.isFirstResponder) {
        NSLog(@"text is first responder");
    }
    NSLog(@"%@:%@--%ld",[noti name],[UIPasteboard generalPasteboard].string,[UIPasteboard generalPasteboard].changeCount);
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"Begin:  %@--%ld",[UIPasteboard generalPasteboard].string,[UIPasteboard generalPasteboard].changeCount);
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"End: %@--%ld",[UIPasteboard generalPasteboard].string,[UIPasteboard generalPasteboard].changeCount);
}

@end

the logs as follow:


2020-04-13 16:31:55.297575+0800 test-ios[3755:876456] UIApplicationWillEnterForegroundNotification:Copyright--72132
2020-04-13 16:31:55.315226+0800 test-ios[3755:876456] UIApplicationWillEnterForegroundNotification:Copyright--72132
2020-04-13 16:31:55.343921+0800 test-ios[3755:876456] UIApplicationDidBecomeActiveNotification:Copyright--72132
2020-04-13 16:31:55.353844+0800 test-ios[3755:876456] UIApplicationDidBecomeActiveNotification:Copyright--72132

2020-04-13 16:31:59.140726+0800 test-ios[3755:876456] Begin:  Copyright--72132
2020-04-13 16:31:59.276366+0800 test-ios[3755:876456] UIKeyboardWillShowNotification:Copyright--72132
2020-04-13 16:31:59.276500+0800 test-ios[3755:876456] text is first responder
2020-04-13 16:31:59.295168+0800 test-ios[3755:876456] UIKeyboardWillShowNotification:Copyright--72132
2020-04-13 16:31:59.673213+0800 test-ios[3755:876456] UIKeyboardWillShowNotification:Copyright--72134
2020-04-13 16:31:59.673338+0800 test-ios[3755:876456] text is first responder
2020-04-13 16:31:59.686831+0800 test-ios[3755:876456] UIKeyboardWillShowNotification:Copyright--72134
2020-04-13 16:31:59.714956+0800 test-ios[3755:876456] UIKeyboardWillShowNotification:Copyright--72134
2020-04-13 16:31:59.715068+0800 test-ios[3755:876456] text is first responder
2020-04-13 16:31:59.725141+0800 test-ios[3755:876456] UIKeyboardWillShowNotification:Copyright--72134
2020-04-13 16:32:32.902285+0800 test-ios[3755:876456] End: Copyright--72134

2020-04-13 16:32:36.060443+0800 test-ios[3755:876456] Begin:  Copyright--72134
2020-04-13 16:32:36.096412+0800 test-ios[3755:876456] UIKeyboardWillShowNotification:Copyright--72134
2020-04-13 16:32:36.096522+0800 test-ios[3755:876456] text is first responder
2020-04-13 16:32:36.105762+0800 test-ios[3755:876456] UIKeyboardWillShowNotification:Copyright--72134
2020-04-13 16:32:39.273601+0800 test-ios[3755:876456] End: Copyright--72136


as it showing, everytime textfield become first responder, pasteboard's chagecount increase,but i don't receive any notification about pasteboard's content changed

Replies

Could you show all the code that deal with the pasteboard ?

hello, i have update my question and add demo code, thank for your focus!

I would suspect that you have several notifications sent:

didReceiveNoti

and

textFieldDidBeginEditing


Could you show an example of the log you get when UITextfield become first responder ?

  • @Claude31 This issue is still easy to reproduce, you can put a few text fields on a VC, and add the next part of the code:

    extension ViewController: UITextFieldDelegate { func textFieldDidBeginEditing(_ textField: UITextField) { print("🔥", UIPasteboard.general.changeCount) } }

    And try to switch between text fields, you will see in the console something like this: 🔥 3699 🔥 3701 🔥 3701 🔥 3701 🔥 3701 🔥 3703 🔥 3703 🔥 3705 🔥 3705

    Why it's happening and how we can avoid this?

Add a Comment

@Claude31 here is a gist with a sample that you can try, just add a CustomTextFieldsView to your VC and try