【问题标题】:iOS 9 UIPasteboard won't work in the backgroundiOS 9 UIPasteboard 无法在后台运行
【发布时间】:2015-11-02 10:17:33
【问题描述】:

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

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

【问题讨论】:

  • 我也遇到了这个问题,除了尝试获取 [UIPasteboard generalPasteboard].image 也返回 null。这使得键盘扩展应用程序现在变得非常无用。希望这是一个错误或在真正的 iOS 9 发布之前找到解决方案。
  • 不确定你是否看过这篇关于同一问题的帖子,人们说这可能是 Beta 5 中引入的错误。Pasting Bug
  • 我在设备控制台中收到“:不允许捆绑 ID (myapp.bundleid) 访问粘贴板而不是前台”。我认为在 iOS 9 中无法在后台获取粘贴板数据。
  • 刚试过,Xcode 7 GM 在扩展中修复了这个问题 =^)
  • Today Widget 在 7 GM 中还可以,但在后台任务中仍然无法这样做。

标签: background clipboard ios9


【解决方案1】:

你能解释一下你在哪里启动generalPasteboard吗?

这就是我要做的:

在您的应用代理的 applicationdidBecomeActive 方法中输入以下代码:

[[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil];

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

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

- (void)returnFromBg {
       UIPasteboard *appPasteBoard = [UIPasteboard generalPasteboard];
       yourTextField.text = [appPasteBoard string;
}

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

[[NSNotificationCenter defaultCenter] removeObserver:self];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多