在后台运行时像Pastebot一样抓住UIPasteboard

| 我知道这是可能的,因为Tapbots Pastebot可以做到这一点。我试图在iPhone应用程序在后台运行时抓取UIPasteboard,然后像Pastebot一样将其添加到UITableView中,但是我也试图缩短链接(如果它是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文档。如果有人有解决方案,可以与我分享吗? 谢谢     
已邀请:
我设法达到类似目的的唯一方法是不打扰
NSNotificationCenter
,而只是在后台定期复制
UIPasteboard
的内容。 下面的代码每秒检查一次“ 2”,持续一千秒钟。我相信一个应用程序可以在后台运行大约10分钟而无需播放音频。如果您在后台播放音频文件,则应用程序可以继续运行。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Create a background task identifier
    __block UIBackgroundTaskIdentifier task; 
    task = [application beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@\"System terminated background task early\"); 
        [application endBackgroundTask:task];
    }];

    // If the system refuses to allow the task return
    if (task == UIBackgroundTaskInvalid)
    {
        NSLog(@\"System refuses to allow background task\");
        return;
    }

    // Do the task
    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        NSString *pastboardContents = nil;

        for (int i = 0; i < 1000; i++) 
        {
            if (![pastboardContents isEqualToString:[UIPasteboard generalPasteboard].string]) 
            {
                pastboardContents = [UIPasteboard generalPasteboard].string;
                NSLog(@\"Pasteboard Contents: %@\", pastboardContents);
            }

            // Wait some time before going to the beginning of the loop
            [NSThread sleepForTimeInterval:1];
        }

        // End the task
        [application endBackgroundTask:task];
    });


}
    
Tapbot实际上在几个月前就在他们的博客上写了一篇文章,介绍了他们用来在后台获取剪贴板的技巧。我自己没有使用该应用程序,因此无法验证该应用程序是否成功,但这是相关的博客条目。     
我知道这有点老。但我想与您分享: http://blog.daanraman.com/coding/monitor-the-ios-pasteboard-while-running-in-the-background/#comment-15135 但是,我怀疑苹果在尝试将其提交到App Store时是否会拒绝这样做,因为在我看来,这就像黑客一样。苹果公司努力避免其整个后台多任务处理的黑客。 有人对此有想法吗?     

要回复问题请先登录注册