CKContainer.discoverAllIdentities always fails

前端 未结 3 1562
别那么骄傲
别那么骄傲 2021-02-14 23:08

The CKContainer.discoverAllIdentities request always fails in my CloudKit app. It has continually failed over the course of several days.

A simplified versi

相关标签:
3条回答
  • 2021-02-14 23:36

    Problem:

    This was a problem in iOS 11.0

    Based on my testing:

    This works ok in Xcode 9.2 / iOS 11.2.1 on the device (not simulator)

    After resetting the simulator works for the first time, doesn't work subsequently, however on the device it works repeatedly.

    Code:

    let queue = OperationQueue()
    
    func requestPermissions(for permissions: CKApplicationPermissions,
                            completionHandler: @escaping (CKApplicationPermissionStatus, Error?) -> ()) {
        
        CKContainer.default().requestApplicationPermission(permissions) { status, error in
            
            if let error = error {
                
                print("Error for requesting \(permissions) - \(error)")
            }
            
            let statusMessage : String
            
            switch status {
                
            case .granted:
                statusMessage = "Granted"
            case .denied:
                statusMessage = "Denied"
            case .couldNotComplete:
                statusMessage = "Could not complete"
            case .initialState:
                statusMessage = "Initial state"
            }
            
            print("Permission - \(statusMessage)")
            
            completionHandler(status, error)
        }
    }
    
    
    private func discoverAllUsers() {
        
        let operation = CKDiscoverAllUserIdentitiesOperation()
        
        operation.userIdentityDiscoveredBlock = { userIdentity in
            
            print("userIdentity = \(userIdentity)")
        }
        
        operation.discoverAllUserIdentitiesCompletionBlock = { error in
            
            if let error = error {
                
                print("Discover all users Error: \(error) ")
            }
            else {
                print("Discover all users completed successfully")
            }
        }
        
        queue.addOperation(operation)
    }
    
    0 讨论(0)
  • 2021-02-14 23:48

    Just another data point on this that might help with the overall picture. I was still seeing this error on 11.2.5 when I used my own iCloud AppleID (with hundreds of contacts) while running a Test App that called discoverAllIdentitiesWithCompletionHandler. I'd get the dreaded CKError 0x1c0051730: "Server Rejected Request" (15/2000); server message = "Internal server error". When I switched to run the exact same code on my daughters iOS11.2.5 device (with just a handful of contacts) the code worked fine. Leads me to believe there is some rate limiting going on when there are a lot of contacts with iOS11. (P.S. No errors at all running on iOS10)

    0 讨论(0)
  • 2021-02-14 23:55

    Edit:

    Apple fixed this issue day after this answer was posted, coincidence?! I don't think so :)


    This is not actually the answer to the question, but a fix that helped me to cross over this error. It will require you to change your app UI interaction and add ContactsUI framework to your project, moreover your user will be responsible for selecting a contact with iCloud related email.

    Good news is that the method discoverUserIdentity is still works. So, you can use it to get CKUserIdentity from manually selected contact.

    func addContact(_ contact:CNContact) {
    
         var lookUpEmails = [CKUserIdentityLookupInfo]()
         for email in contact.emailAddresses {
             lookUpEmails.append(CKUserIdentityLookupInfo(emailAddress: (email.value as String)))
         }
    
         let checkUserOperation = CKDiscoverUserIdentitiesOperation()
            
         checkUserOperation.userIdentityLookupInfos = lookUpEmails
            
         checkUserOperation.userIdentityDiscoveredBlock = { [unowned self] (identity, info) -> Void in
             if identity.hasiCloudAccount {
                 if let recordID = identity.userRecordID {
                      //do something with discovered user
                 }
                 checkUserOperation.cancel()
             }
         }
         checkUserOperation.queuePriority = Operation.QueuePriority.high
         CKContainer.default().add(checkUserOperation)
    }
    

    It might sound useless, but in my case, it helped me to solve the Server Rejected Request" (15/2000) error, to fix one of the features of my app and continue to use the other feature related code with less efforts than I thought.

    I hope someone will find this helpful.

    0 讨论(0)
提交回复
热议问题