Grabbing the UIPasteboard like Pastebot while running in the background

穿精又带淫゛_ 提交于 2019-12-03 17:16:09

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.

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


}

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.

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!