Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 489 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if User is Logged into iCloud? Swift/iOS

#1
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?
Reply

#2
Here Solution for Swift 5.x

func isICloudContainerAvailable()->Bool {
if let _ = FileManager.default.ubiquityIdentityToken {
return true
} else {
return false
}
}
Reply

#3
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")
}

If the `ubiquityIdentityToken` is `nil` and 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")
}
}

Note that this requires the use of CloudKit, which requires the CloudKit entitlement:

```plist
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudKit</string>
</array>
```

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")
}
}
Reply

#4
There are **two methods** for checking iCloud functionalities, which are provided for **two different needs**.

1. Checking for **iCloudDrive** availability
2. Checking for **iCloud CKContainer** availability

## Checking for iCloudDrive availability ##

From Apples documentation:

**FileManager.default.ubiquityIdentityToken** -> *An opaque token that represents the current user’s iCloud Drive Documents identity.*

*In iCloud Drive Documents, when iCloud is available, this property contains an opaque object representing the identity of the current user. If iCloud is unavailable or there is no logged-in user, the value of this property is nil.*

To check for this iCloud functionality we can retrieve that token and check for *nil*.

```
// Request iCloud token
let token = FileManager.default.ubiquityIdentityToken
if token == nil {
print("iCloud (Drive) is not available")
} else {
print("iCloud (Drive) is available")
}
```
To assure beeing notified, if iCloudDrive availabilty changes during the app runs -> register to the NotificationCenter for *NSUbiquityIdentityDidChange* notification.

## Checking for iCloud CKContainer availability ##

To check, wether the users iCloud account is available for accessing the **CKContainer** (and its **private database**), we can use an async request on the *default* container.
```
// Check iCloud account status (access to the apps private database)
CKContainer.default().accountStatus { (accountStatus, error) in

if accountStatus == .available {
print("iCloud app container and private database is available")
} else {
print("iCloud not available \(String(describing: error?.localizedDescription))")
}
}
```
To be informed about changes while the app is running, you can use the *CKAccountChanged* notification.
Reply

#5
I think this async method is preferred so that you don't block while you are checking.

CKContainer.defaultContainer().accountStatusWithCompletionHandler { (accountStat, error) in
if (accountStat == .Available) {
print("iCloud is available")
}
else {
print("iCloud is not available")
}
}
Reply

#6
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through