swift – CKContainer.discoverAllIdentities总是失败

前端之家收集整理的这篇文章主要介绍了swift – CKContainer.discoverAllIdentities总是失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的CloudKit应用程序中的CKContainer.discoverAllIdentities请求总是失败.它在几天的过程中不断失败.

失败的代码的简化版本(导致相同的错误)是:

private func getContacts(completion: (([CKUserIdentity]?) -> Void)?) {
    container.status(forApplicationPermission: .userDiscoverability) { [weak self] status,error in
        if let error = error {
            print(error)
        }

        switch status {
        case .granted:
            self?.discover(completion: completion)
        default:
            print("status not granted")
        }
    }
}

private func discover(completion: (([CKUserIdentity]?) -> Void)?) {
    let op = CKDiscoverAllUserIdentitiesOperation()
    op.qualityOfService = .userInitiated
    op.discoverAllUserIdentitiesCompletionBlock = { error in
        if let error = error {
            print(error)
        }
    }
    op.userIdentityDiscoveredBlock = { identity in
        print(identity)
    }
    op.start()
}

它会导致错误传递给op.discoverAllUserIdentitiesCompletionBlock.日志中的错误描述是:

< CKError 0x1c4a51a60:“服务器拒绝请求”(15/2000); server message =“内部服务器错误”; uuid = F67453B9-712D-4E5E-9335-929123E3C978; container ID =“iCloud.com.huntermaximillionmonk.topdraw”>

以前,此操作可以使用,但仅适用于某些iCloud用户.现在不适合我的两个测试用户.

问题:

这是iOS 11.0中的一个问题

根据我的测试:

这在设备上的Xcode 9.2 / iOS 11.2.1中工作正常(不是模拟器)

重置模拟器后第一次工作,随后不起作用,但是在设备上它重复工作.

码:

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)
}
原文链接:https://www.f2er.com/swift/319440.html

猜你在找的Swift相关文章