【问题标题】:NSUbiquityIdentityDidChangeNotification and SIGKILLNSUbiquityIdentityDidChangeNotification 和 SIGKILL
【发布时间】:2015-06-15 06:50:48
【问题描述】:

斯威夫特 1.2 Xcode 6

长期倾听者,第一次来电者。

你好,

直言不讳:“要处理 iCloud 可用性的变化,请注册以接收 NSUbiquityIdentityDidChangeNotification 通知。”

这是他们提供的用于实现此功能的代码:

[[NSNotificationCenter defaultCenter]

addObserver: self

   selector: @selector (iCloudAccountAvailabilityChanged:)

       name: NSUbiquityIdentityDidChangeNotification

     object: nil];

我在我的应用中将其 Swiftified 为:

var observer = NSNotificationCenter.defaultCenter().addObserverForName
(NSUbiquityIdentityDidChangeNotification, object: nil, queue: NSOperationQueue.mainQueue()
){...completion block...}

源代码:https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/Chapters/iCloudFundametals.html#//apple_ref/doc/uid/TP40012094-CH6-SW6

实现这一点的正确方法是什么?它在 AppDelegate 中吗?当应用被发送到后台时,我们会移除观察者吗?

我遇到的问题是,当 Ubiquity Token 更改时,应用程序无论如何都会终止,因为用户更改了 iCloud 设置。

你们是如何设法订阅此通知的,如果不订阅,您会做什么来跟踪当前登录的 iCloud 用户?

谢谢!

【问题讨论】:

    标签: ios xcode swift icloud


    【解决方案1】:

    我发现了同样的问题,请参阅 this question 了解我的 cmets。

    总结一下:在 iOS 上,我认为应用程序在 iCloud 帐户更改(或刚刚注销)时无论如何都会被终止。所以不用听NSUbiquityIdentityDidChangeNotification

    但是,如果您使用的是 tvOS,则您的应用不会被终止。当您的应用程序激活时,您会收到一个或多个 NSUbiquitousKeyValueStoreDidChangeExternallyNotification 并将 NSUbiquitousKeyValueStoreChangeReasonKey 设置为 NSUbiquitousKeyValueStoreAccountChange,因为 tvOS 会交换您的整个无处不在的键值存储。

    也许您可以使用一些技巧来检测帐户更改,例如在ubiquity key-value-store中存储一个NSUUID,当值发生变化时,表示有一个新账户。但是您无法检测到帐户是否已注销。

    【讨论】:

      【解决方案2】:

      简答

      要在用户在使用您的应用时登录或退出 iCloud 时在 iOS 中收到通知,请使用 CKAccountChangedNotification,而不是 NSUbiquityIdentityChanged

      长答案

      我也一直在努力让它发挥作用。我记得在 WWDC16 的一次演讲中,他们推荐使用 something 这样的东西。但是,从他们提供的示例代码中,我只能找到NSUbiquityKeyIdentityChanged,我一直无法开始工作。

      所以我回到the video (it's from 2015)。这就是我看到他们引用CKAccountChangedNotification 的地方——它完全按预期工作:

      • 从 Xcode 在模拟器上启动您的应用
      • 退出模拟器上的设置应用
      • 登录(或注销)iCloud 帐户
      • 返回您的应用程序(点击模拟器主屏幕上的图标)
        • 收到通知。
      • 再次退出“设置”应用
      • 注销(或登录)iCloud 帐户
      • 再次返回您的应用程序
        • 收到另一个通知。

      【讨论】:

      • 谢谢你。有很多资源指向 NSUbiquity 通知,但指向这一点的资源还不够多。 任何使用 CloudKit 的人都应该使用 CKAccountChangedNotification。
      • 那么,iOS 会在应用返回前台时发送通知?
      • @NicolasMiari 是的,对我来说,在我的应用程序回到前台之前我不会得到CKAccountChangedNotification
      • 因此,一旦您收到CKAccountChanged 通知,您是否仍然检查FileManager.default.ubiquityIdentityToken 以查看iCloud 是否可用?还是有更新的同步方式来检查?
      • 我已经尝试过这个解决方案,但是在应用程序终止之前没有调用 Selector 方法。我已经尝试使用这个和NSUbiquityIdentityDidChange 来观察用户何时为我的应用打开/关闭 iCloud 支持的变化。似乎没有优雅的方式来处理这个......
      【解决方案3】:

      Swift 3.0 中有另一个重命名:

      现在NSUbiquityIdentityDidChangeNotification 已更改为NSNotification.Name.NSUbiquityIdentityDidChange

      所以完整的注册如下:

      // Register for iCloud availability changes
      NotificationCenter.default.addObserver(self, selector: #selector(...), name: NSNotification.Name.NSUbiquityIdentityDidChange, object: nil)
      

      【讨论】:

        【解决方案4】:

        在 iOS 10 上,我发现 NSUbiquityIdentityDidChangeNotification 从未发送过。如果我有一个 CKContainer(根据文档),CKAccountChangedNotification 是在非常有限的情况下发送的。

        使用 xCode 9.1 构建,然后在 iOS 10.02 iPhone 6+ 和 iOS 11.0.3 iPhone SE 上进行测试

        CKAccountChangedNotification 已发送,如果

        • 用户已登录 iCloud 帐户,或
        • 用户在 iOS 11 中启用了 iCloud Drive。这总是导致 iCloud Drive->App 被启用。但是,之后获取帐户状态会产生 NoAccount!
        • 用户在 iOS 10 中启用了 iCloud Drive。iCloud Drive->App 的后续状态与我禁用 iCloud Drive 时的状态相同。帐户状态是适当的。但是,如果此时禁用 iCloud Drive->App,启用它不会产生终止或通知。

        应用程序被终止如果

        • 无论 iCloud Drive 状态如何,用户都已从 iCloud 注销
        • 用户禁用了 iCloud Drive->App
        • 用户禁用了 iCloud Drive(即使 iCloud Drive->App 已经禁用)
        • 用户在启用 iCloud Drive 的情况下启动应用程序,然后启用 iCloud Drive->App

        【讨论】:

          猜你喜欢
          • 2013-07-03
          • 1970-01-01
          • 2012-10-29
          • 2019-10-25
          • 1970-01-01
          • 2017-03-01
          • 2013-12-17
          • 1970-01-01
          • 2014-07-28
          相关资源
          最近更新 更多