iOS 9 UIPasteboard无法在后台运行

Shi*_*ama 18 clipboard background ios9

[UIPasteboard generalPasteboard].string当应用程序在后台运行后台任务或今日小部件时,iOS 9 将变为null.

我们不能再在后台检索剪贴板文本了吗?

Ale*_*ano 1

您能解释一下在哪里启动 GeneralPasteboard 吗?

这就是我要做的:

在您的应用程序委托的 applicationdidBecomeActive 方法中放入以下代码:

[[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil];
Run Code Online (Sandbox Code Playgroud)

接下来,在当前活动视图控制器的 init 方法中订阅通知。

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(returnFromBg)        
                                             name:@"appDidBecomeActive" 
                                             object:nil];

- (void)returnFromBg {
       UIPasteboard *appPasteBoard = [UIPasteboard generalPasteboard];
       yourTextField.text = [appPasteBoard string;
}
Run Code Online (Sandbox Code Playgroud)

PS 当视图控制器被移除时,不要忘记移除观察者:

[[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)