3

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.

Phineas Lue
  • 317
  • 4
  • 15
  • How long can you keep the loop running in background? – Allen Hsu Dec 28 '12 at 07:53
  • Then I think it's not a good solution. Users never known when and which strings they've copied will be translated. – Allen Hsu Dec 28 '12 at 09:01
  • Actually, because it would check new copy per second, that is normally more frequent than user copy operations, user may feel that it just react to copy operation instantly. And I think we can use some method to make it run in background forever, as mentioned below. – Phineas Lue Jan 04 '13 at 03:06
  • As I known, it's impossible to make an iOS app run in background forever, did you found out anything interesting? – saiday Dec 03 '13 at 17:54

1 Answers1

1

A possible method is to "monitor" UIPasteboard when the app is in background for regular time intervals, say 5 secs, and then it's possible to know if there's a change in clipboard. In order to make sure the copy content is a string, check [[UIPasteboard generalPasteboard] string]

Yet background task is limited to 600secs, (and playing mute music in background may get you rejected), so there are discussions over how to extend the time. Also, Pasterbot has declared a similar, method, see here.

Community
  • 1
  • 1
BabyPanda
  • 1,562
  • 12
  • 18