如何在手机处于飞行模式时检测iCloud用户?

时间:2016-09-19 13:17:18

标签: ios swift cloudkit userid

首先我知道这篇文章:How to identify iCloud logged in user in airplane mode?

但它似乎没有答案。一个答案是关于检测互联网连接,另一个是关于用户是否已经改变的讨论。

这是我的代码:

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

看起来非常愚蠢,你需要一个互联网连接来查看用户是否登录到iCloud。我该如何解决这个问题?

谢谢!

P.S。 如果其他帖子确实包含我的问题的答案,请您为我突出显示它?我找不到它。干杯!

1 个答案:

答案 0 :(得分:0)

飞行模式只是关闭蜂窝和wifi数据功能。在这种情况下,检查可达性应该与检测飞行模式的目的相同。这是一个标准的lib片段,用于检查是否具有可访问性:

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