不使用iCloudDrive时CKAccountStatus?

时间:2017-04-13 23:24:38

标签: cloudkit

我正在使用CloudKit。我使用此代码检查用户是否登录到iCloud。

[[CKContainer containerWithIdentifier:my.container.id] 
   accountStatusWithCompletionHandler:^(CKAccountStatus accountStatus, NSError *error) {
  // check accountStatus         
}];

但是,如果我已登录,则帐户状态将显示为CKAccountStatusNoAccount,但已关闭iCloud Drive。我希望它是CKAccountStatusAvailable,因为我在这里没有使用iCloud Drive。

有没有办法检查用户是否登录,无论iCloud Drive使用情况如何?

1 个答案:

答案 0 :(得分:0)

此代码检查用户是否有权访问iCloud。这将有效。

 func isAuthorized4Cloud() {
    appDelegate = UIApplication.shared.delegate as! AppDelegate
    container = CKContainer(identifier: "iCloud.ch.cqd.presenTagger")
    publicDB = container.publicCloudDatabase
    privateDB = container.privateCloudDatabase
    var userID: CKRecordID!
    container.fetchUserRecordID( completionHandler: { recordID, error in
        guard error == nil else {
            if let ckerror = error as? CKError {
                if ckerror.code == CKError.requestRateLimited {
                    let retryInterval = ckerror.userInfo[CKErrorRetryAfterKey] as? TimeInterval
                    DispatchQueue.main.async {
                        Timer.scheduledTimer(timeInterval: retryInterval!, target: self, selector: #selector(self.files_searchSet), userInfo: nil, repeats: false)
                    }
                } else if ckerror.code == CKError.zoneBusy {
                    let retryInterval = ckerror.userInfo[CKErrorRetryAfterKey] as? TimeInterval
                    DispatchQueue.main.async {
                        Timer.scheduledTimer(timeInterval: retryInterval!, target: self, selector: #selector(self.files_searchSet), userInfo: nil, repeats: false)
                    }
                } else if ckerror.code == CKError.limitExceeded {
                    let retryInterval = ckerror.userInfo[CKErrorRetryAfterKey] as? TimeInterval
                    DispatchQueue.main.async {
                        Timer.scheduledTimer(timeInterval: retryInterval!, target: self, selector: #selector(self.files_searchSet), userInfo: nil, repeats: false)
                    }
                } else if ckerror.code == CKError.notAuthenticated {
                    NotificationCenter.default.post(name: Notification.Name("noCloud"), object: nil, userInfo: nil)
                } else if ckerror.code == CKError.networkFailure {
                    NotificationCenter.default.post(name: Notification.Name("networkFailure"), object: nil, userInfo: nil)
                } else if ckerror.code == CKError.networkUnavailable {
                    NotificationCenter.default.post(name: Notification.Name("noWiFi"), object: nil, userInfo: nil)
                } else if ckerror.code == CKError.quotaExceeded {
                    NotificationCenter.default.post(name: Notification.Name("quotaExceeded"), object: nil, userInfo: nil)
                } else if ckerror.code == CKError.partialFailure {
                    NotificationCenter.default.post(name: Notification.Name("partialFailure"), object: nil, userInfo: nil)
                } else if (ckerror.code == CKError.internalError || ckerror.code == CKError.serviceUnavailable) {
                    NotificationCenter.default.post(name: Notification.Name("serviceUnavailable"), object: nil, userInfo: nil)
                }
            } // end of guard statement
            return
        }

        if error == nil {
            userID = recordID
            //print("fcuk1210 files_setup userID \(userID.recordName)")
            NotificationCenter.default.post(name: Notification.Name("cloudConnected"), object: nil, userInfo: nil)
        }
    })
}