Check if User is Logged into iCloud? Swift/iOS

后端 未结 4 1868
后悔当初
后悔当初 2020-12-24 06:10

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 logg

4条回答
  •  遥遥无期
    2020-12-24 06:27

    If you just want to know if the user is logged in to iCloud, the synchronous method can be used:

    if FileManager.default.ubiquityIdentityToken != nil {
        print("iCloud Available")
    } else {
        print("iCloud Unavailable")
    }
    

    However, if you'd like to know why iCloud isn't available, you can use the asynchronous method:

    CKContainer.default().accountStatus { (accountStatus, error) in
        switch accountStatus {
        case .available:
            print("iCloud Available")
        case .noAccount:
            print("No iCloud account")
        case .restricted:
            print("iCloud restricted")
        case .couldNotDetermine:
            print("Unable to determine iCloud status")
        }
    }
    

    If you want to use the asynchronous method but don't care about why, you should check that accountStatus is available, rather than checking that it is not noAccount:

    CKContainer.default().accountStatus { (accountStatus, error) in
        if case .available = accountStatus {
            print("iCloud Available")
        } else {
            print("iCloud Unavailable")
        }
    }
    

提交回复
热议问题