iOS 9 UIPasteboard won't work in the background

余生长醉 提交于 2019-12-01 15:44:49

Can you explain where do you launch generalPasteboard?.

This is what I would do:

In your app delegate's applicationdidBecomeActive method put in this code:

[[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil];

Next , in your current active view controller's init method subscribe to the notification.

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(returnFromBg)        
                                             name:@"appDidBecomeActive" 
                                             object:nil];

- (void)returnFromBg {
       UIPasteboard *appPasteBoard = [UIPasteboard generalPasteboard];
       yourTextField.text = [appPasteBoard string;
}

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

[[NSNotificationCenter defaultCenter] removeObserver:self];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!