I have seen an iphone app called CopyTranslate. When we copy something, e.g. in safari, it can give an translation of what we copied by a local notification.
So I am wondering if app can receive UIPasteboardChangedNotification even in background. Then I make a test that I build a app which can run in background for 10 minutes, and observe to UIPasteboardChangedNotification. The result is that app can not get notification in background.
Can anyone have ideas that how CopyTranslate implement its function like that? Does it check the UIPasetBoard per second? If thus, how does it know there is a new string copy instead of image copy or something else.
Thank you for any idea!
I have solved this problem by checking the changeCount of UIPasteboard per second in background.
Here are details:
I hold a property lastChangeCount in AppDelegate, and when my application comes into background, I start a infinite while loop which sleep for 1 second per iteration. And I will compare the UIPasteboard's changeCount with lastChangeCount per iteration, if they are not equal, then I know there is some new paste items in pasteboard. So I will get the string property of UIPasteboard, if it exists, I schedule a local notification to show it. If not, I means that the user may copy a image or something else, so I will do nothing.