【问题标题】:NSUbiquityIdentityDidChangeNotification does not fire with cloud kit云套件不会触发 NSUbiquityIdentityDidChangeNotification
【发布时间】:2015-07-07 16:02:34
【问题描述】:

这是我当前的代码

func applicationDidBecameActive(notification:NSNotification) {

    println("Application is active")

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleIdentityChange:", name: NSUbiquityIdentityDidChangeNotification, object: nil)

}

func applicationBecameInactive(notification:NSNotification) {

    println("Application is inactive")

    NSNotificationCenter.defaultCenter().removeObserver(self, name: NSUbiquityIdentityDidChangeNotification, object: nil)

}

func handleIdentityChange(notification:NSNotification) {

    println("this is working")

    let fileManager = NSFileManager()

    if let token = fileManager.ubiquityIdentityToken {

        println("New token is \(token)")

    }
    else {

        println("User has logged out of iCloud")

    }

}

“应用程序处于活动状态”和“应用程序处于非活动状态”正常工作。那里没有问题。

我无法让它“这工作正常”。通过登录不同的 iCloud 帐户或退出 iCloud 帐户。

我在模拟器和实际设备上试过。

请帮我解决这个问题或建议替代方法来实现相同的目标(更改 iCloud 帐户)。

【问题讨论】:

  • 如果您想更改其他用户答案的​​含义,而不是对其进行编辑,请在对该答案的评论中说明您认为应该更改的内容,或添加您自己的答案。
  • 尝试在 willEnterForeground 上连接通知处理程序。 didBecomeActive 为时已晚。
  • 我试过了。它仍然不起作用。感谢您的帮助,但我现在在 iOS 9 中使用 CKAccountChangeNotification。

标签: ios swift icloud cloudkit


【解决方案1】:

在 Swift 中,我有时需要在函数前面添加 @objc 以便 NSNotificationCenter 找到它。

所以不是

func handleIdentityChange(notification:NSNotification) {

我会试试的

@objc func handleIdentityChange(notification:NSNotification) {

【讨论】:

    【解决方案2】:

    看看我在this question 中的评论。我也从未收到过NSUbiquityIdentityDidChangeNotification。在 iOS 中,当您更改帐户时,您的应用程序会被终止。在 tvOS 中,您可以使用 NSUbiquitousKeyValueStoreAccountChange

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 2020-03-23
      相关资源
      最近更新 更多