2017-04-13 46 views
0

我與CloudKit工作。我使用此代碼來檢查用戶是否登錄到iCloud。CKAccountStatus iCloudDrive?

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

但是,帳戶狀態回來爲CKAccountStatusNoAccount如果我登錄,但iCloud的驅動器關閉。我希望它是CKAccountStatusAvailable代替,因爲我沒有使用iCloud的驅動器在這裏。

有沒有辦法檢查用戶是否登錄而不管iCloud Drive的使用情況?

回答

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