Grabbing the UIPasteboard like Pastebot while running in the background

Deadly 提交于 2019-12-21 05:34:15

问题


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


回答1:


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


}



回答2:


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.




回答3:


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?



来源:https://stackoverflow.com/questions/5721733/grabbing-the-uipasteboard-like-pastebot-while-running-in-the-background

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