【问题标题】:Grabbing the UIPasteboard like Pastebot while running in the background在后台运行时像 Pastebot 一样抓取 UIPasteboard
【发布时间】:2011-04-19 19:40:47
【问题描述】:

我知道这是可能的,因为 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 文档。如果有人有解决方案,可以分享给我吗?

谢谢

【问题讨论】:

  • Pastebot 到底是如何进入应用商店的?
  • 显然,这个技巧没有违反任何 App Store 规则。它不使用私有 API,所以我会像他们一样收集它,它 100% 安全。太糟糕了,他们不会分享他们的伎俩。
  • 上次我听说,他们有 10 分钟的时间限制。不侵犯任何东西。我能想到的就是在后台时,每次 UIPasteboard 发生变化,然后抓取对象...
  • 我已经尝试了我能想到的一切。希望这可以实现。我怀疑 Tapbots 会分享他们的秘密。
  • 也许可以尝试联系他们,一定要试一试;)

标签: iphone url notifications multitasking uipasteboard


【解决方案1】:

我设法实现类似目标的唯一方法是不打扰NSNotificationCenter,而是在后台定期复制UIPasteboard 的内容。

下面的代码每秒检查一次UIPasteboard,持续一千秒。我相信一个应用程序可以在后台运行大约 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];
    });


}

【讨论】:

    【解决方案2】:

    Tapbots 实际上几个月前在他们的博客上写了一篇文章,讲述了他们用来在后台获取剪贴板的技巧。我自己不使用该应用程序,因此我无法验证这是否已实现,但这是相关的blog entry

    【讨论】:

    • 我确实读过,非常有趣。我已经尝试过了,但没有奏效。他们必须使用任务完成,但我已经尝试过了,但仍然无法正常工作。
    • 您好,Tapbot 博客条目的链接已失效,是否有任何复活石可供开发人员使用?谢谢;-)
    • Apple 最终打击了博客文章使用的方法——播放无声音频文件以保持应用在后台运行。
    【解决方案3】:

    我知道这是一个老话题。但我想和你分享这个:

    http://blog.daanraman.com/coding/monitor-the-ios-pasteboard-while-running-in-the-background/#comment-15135

    但是,我怀疑 Apple 在尝试将其提交到 App Store 时是否会拒绝它,因为我觉得这就像一个黑客。 Apple 努力避免其整个后台多任务处理的黑客攻击。

    有人对此有什么想法吗?

    【讨论】:

      猜你喜欢
      • 2015-11-02
      • 2019-06-26
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      相关资源
      最近更新 更多