0

Firstly I am aware of this post: How to identify iCloud logged in user in airplane mode?

It doesn't seem to have an answer though. One answer talks about detecting Internet connectivity and the other talks about wether or not the user has changed.

Here is my code:

container.accountStatusWithCompletionHandler{
        (status: CKAccountStatus, error: NSError?) in

        dispatch_async(dispatch_get_main_queue(), {

            var title: String!
            var message: String!

            if error != nil{
                title = "Error"
                message = "An error occurred = \(error)"
            } else {
                //title = "No errors occurred getting info"
                switch status{
                case .Available:
                    message = "The user is logged in to iCloud"
                    title = "GOOD"
                    print("determined status was available")
                    self.shouldPullFromICloud()
                    //self.displayAlertWithTitle(title, message: message)
                case .CouldNotDetermine:
                    message = "Could not determine if the user is logged" +
                    " into iCloud or not"
                    title = "BAD"
                    self.noUserIsSignedIn()
                case .NoAccount:
                    message = "User is not logged into iCloud"
                    title = "BAD"
                    self.noUserIsSignedIn()
                case .Restricted:
                    message = "Could not access user's iCloud account information"
                    title = "BAD"
                    self.noUserIsSignedIn()
                }
                print(title, message)
            }
        })
    }

Seems pretty stupid that you need an internet connection to see if a user is signed into iCloud. How can I fix this?

Thanks!

P.S. If that other post does contain the answer to my question can you please highlight it for me? I couldn't find it. Cheers!

Community
  • 1
  • 1
lelephant
  • 95
  • 9

1 Answers1

0

Airplane mode simply turns off cellular and wifi data capabilities. In this case, checking reachability should serve the same purposes as detecting Airplane mode. Here is a standard lib snippet that will check for reachability:

func connectedToNetwork() -> Bool {
    var zeroAddress = sockaddr_in()
    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)

    guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {
        SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
    }) else {
        return false
    }
    var flags : SCNetworkReachabilityFlags = []

    if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == false {
        return false
    }

    let isReachable = flags.contains(.Reachable)
    let needsConnection = flags.contains(.ConnectionRequired)
    return (isReachable && !needsConnection)
}
Jpark822
  • 138
  • 9
  • I am using this for other reasons but I still need a way of getting a user's ID no matter if they are on airplane mode or not ya know? – lelephant Sep 19 '16 at 16:25