gpt4 book ai didi

iphone - 在后台运行时像 Pastebot 一样抓取 UIPasteboard

转载 作者:行者123 更新时间:2023-12-03 19:29:36 27 4
gpt4 key购买 nike

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

谢谢

最佳答案

我成功实现类似目标的唯一方法是不打扰 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];
});


}

关于iphone - 在后台运行时像 Pastebot 一样抓取 UIPasteboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5721733/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com