在后台运行时抓住像Pastebot这样的UIPasteboard [英] Grabbing the UIPasteboard like Pastebot while running in the background

查看:317
本文介绍了在后台运行时抓住像Pastebot这样的UIPasteboard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是可能的,因为Tapbots Pastebot会这样做。当我的iPhone应用程序在后台运行时,我试图抓住UIPasteboard并将其添加到UITableView,就像Pastebot一样,但我也试图缩短链接,如果它是一个URL并将其复制回UIPastboard以便它准备就绪供用户粘贴到任何地方。现在,Pastebot通过播放音频文件10分钟显然在后台运行。我已经在applicationDidFinishLaunching中设置了NSNotificationCenter

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]];
     }
}

任何人都可以指点我抓住UIPasteboard的方向并缩短链接,如果它是一个URL并将其发送回UIPasteboard?我已经阅读了多任务开发文档和UIPasteboard文档。如果有人有解决方案,请与我分享?

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?

谢谢

推荐答案

我设法实现类似功能的唯一方法是不打扰 NSNotificationCenter 而只是复制 UIPasteboard <的内容/ code>在后台定期播放。

The only way I have managed to achieve something similar is by not bothering with the NSNotificationCenter and instead just copying the contents of the UIPasteboard at regular intervals whilst in the background.

下面的代码检查 UIPasteboard 秒数千秒。我相信应用程序可以在后台运行大约10分钟而不播放音频。如果你在后台播放音频文件,应用程序可以继续运行。

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.

- (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];
    });


}

这篇关于在后台运行时抓住像Pastebot这样的UIPasteboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆