Search code examples
iosswifticloudcloudkit

Check if User is Logged into iCloud? Swift/iOS


Is there a way for me to check and see if a user is logged into iCloud when they open the app up? I want to be able to direct them to the settings page if they are not logged in, and if they are logged into iCloud and have used the app before - I want to skip the sign in page....

I looked into Apple's iCloud and Cloudkits documentation but was unable to find anything that would be of assistance! Is this even possible to do?


Solution

  • Here you go - hopefully self explanatory. For more look at the Apple docs for the NSFileManager function below.

    func isICloudContainerAvailable()->Bool {
            if let currentToken = NSFileManager.defaultManager().ubiquityIdentityToken {
                return true
            }
            else {
                return false
            }
        }
    

    See extract below: An opaque token that represents the current user’s iCloud identity (read-only) When iCloud is currently available, this property contains an opaque object representing the identity of the current user. If iCloud is unavailable for any reason or there is no logged-in user, the value of this property is nil.