我知道这是可能的,因为 Tapbots Pastebot 就是这样做的。我试图在我的 iPhone 应用程序在后台运行时抓取 UIPasteboard 并将其添加到 UITableView 中,就像 Pastebot 所做的那样,但我也尝试缩短链接(如果它是 URL)并将其复制回 UIPastboard 以便准备就绪供用户粘贴到任何地方。现在,Pastebot 显然在后台运行,播放音频文件 10 分钟。我已经在 applicationDidFinishLaunching 中设置了 NSNotificationCenter,
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pasteboardChangedNotification:) name:UIPasteboardChangedNotification object:[UIPasteboard generalPasteboard]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pasteboardChangedNotification:) name:UIPasteboardRemovedNotification object:[UIPasteboard generalPasteboard]];
- (void)pasteboardChangedNotification:(NSNotification*)notification {
pasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if (pasteboardChangeCount_ != [UIPasteboard generalPasteboard].changeCount) {
[[NSNotificationCenter defaultCenter] postNotificationName:UIPasteboardChangedNotification object:[UIPasteboard generalPasteboard]];
}
}
任何人都可以为我指出抓取 UIPasteboard 并缩短链接(如果它是 URL 并将其发送回 UIPasteboard)的方向吗?我已阅读多任务开发文档和 UIPasteboard 文档。如果有人有解决方案,可以与我分享吗?
谢谢
I know this is possible as Tapbots Pastebot does this. I am trying to grab the UIPasteboard when my iPhone app is running in the background and add it to a UITableView just as Pastebot does but I am also trying to shorten the link, if its a URL and copy that back to the UIPastboard so its ready for the user to paste anywhere. Now Pastebot apparently runs in the background by playing an audio file for 10 minutes. I have set up the NSNotificationCenter like so in the applicationDidFinishLaunching
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pasteboardChangedNotification:) name:UIPasteboardChangedNotification object:[UIPasteboard generalPasteboard]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pasteboardChangedNotification:) name:UIPasteboardRemovedNotification object:[UIPasteboard generalPasteboard]];
- (void)pasteboardChangedNotification:(NSNotification*)notification {
pasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if (pasteboardChangeCount_ != [UIPasteboard generalPasteboard].changeCount) {
[[NSNotificationCenter defaultCenter] postNotificationName:UIPasteboardChangedNotification object:[UIPasteboard generalPasteboard]];
}
}
Can anyone point me in a direction on grabbing the UIPasteboard and shortening a link, if its a URL and sending it back to the UIPasteboard? I have read the multitasking dev documents and the UIPasteboard documents. If anyone has a solution can you please share them with me?
Thanks
发布评论
评论(3)
我成功实现类似目标的唯一方法是不使用 NSNotificationCenter,而是在后台定期复制 UIPasteboard 的内容。
下面的代码每秒检查一次
UIPasteboard
,持续一千秒。我相信应用程序可以在后台运行大约 10 分钟而不播放音频。如果您在后台播放音频文件,应用程序可以继续运行。The only way I have managed to achieve something similar is by not bothering with the
NSNotificationCenter
and instead just copying the contents of theUIPasteboard
at regular intervals whilst in the background.The code bellow checks the
UIPasteboard
once a second for a thousand seconds. I believe that an application can run in the background for around 10 minutes without playing audio. If you play an audio file in the background the application can keep running.事实上,几个月前,Tapbots 在他们的博客上写了一篇关于他们在后台获取剪贴板的技巧的文章。我自己不使用该应用程序,所以我无法验证这是否已经实现,但这是相关的 博客条目。
Tapbots actually wrote an entry on their blog a few months back about the trick they use to get at the clipboard in the background. I don't use the app myself, so I can't verify that this ever came to fruition, but here's the relevant blog entry.
我知道这是一个旧线程。但我想与您分享:
http://blog.daanraman.com/coding/monitor-the-ios-pasteboard-while-running-in-the-background/#comment-15135
但是,我有我的怀疑苹果在尝试将其提交到应用商店时是否会拒绝它,因为在我看来这就像黑客攻击。苹果在后台多任务处理方面极力避免这种黑客行为。
有人想过吗?
I know this is kind of an old thread. But I'd like to share this with you:
http://blog.daanraman.com/coding/monitor-the-ios-pasteboard-while-running-in-the-background/#comment-15135
However, I have my doubts whether Apple will reject this when trying to submit it to the App Store, as it feels to me like a hack. A hack that Apple tries hard to avoid with its whole background multitasking thing.
Anyone thoughts about it?