微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

容器限制后,iCloud 仍然能够保存记录

如何解决容器限制后,iCloud 仍然能够保存记录

我对我的 cloudkit 公共容器实施了限制,但我仍然能够保存 CKRecords。

在这里复制粘贴了苹果教程中的代码(我用我的容器特有的必要代码替换了样板代码

https://developer.apple.com/documentation/cloudkit/changing_access_controls_on_user_data/

我收到“限制成功”消息,但是当我将记录保存到容器时..它会通过并且新记录出现在 cloudkit 仪表板查询

    func createString() {
    let record = CKRecord(recordtype: "TestString")
    
    record["testString"] = "Hello"
    container.publiccloudDatabase.save(record) { record,error in
        if error != nil {
            print(error!)
        }else {
            print(record!)
        }
    }
}

这里是所有代码

class ViewController: UIViewController {

let container = CKContainer(identifier: "***")
var containerAPITokens: [CKContainer: String] = [:]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    containerAPITokens = [
        container: "***"
    ]
    
    let apiToken = containerAPITokens[container]
    
    let fetchAuthorization = CKFetchWebAuthTokenoperation(apiToken: apiToken!)
        
        fetchAuthorization.fetchWebAuthTokenCompletionBlock = { webToken,error in
            guard let webToken = webToken,error == nil else { return }

            self.restrict(container: self.container,apiToken: apiToken!,webToken: webToken) { error in
                guard error == nil else {
                     print("Restriction Failed. Reason: ",error!)
                     return
                }
                print("Restriction succeeded.")
            }

        }
        
        container.publiccloudDatabase.add(fetchAuthorization)
}

@IBAction func createStringButtonTapped(_ sender: Any) {
    createString()
}

func restrict(container: CKContainer,apiToken: String,webToken: String,completionHandler: @escaping (Error?) -> Void) {
    let webToken = encodetoken(webToken)
    
    let identifier = container.containerIdentifier!
    let env = "development" // Use "development" during development.
    let baseURL = "https://api.apple-cloudkit.com/database/1/"
    let apiPath = "\(identifier)/\(env)/private/users/restrict"
    let query = "?ckAPIToken=\(apiToken)&ckWebAuthToken=\(webToken)"
    
    let url = URL(string: "\(baseURL)\(apiPath)\(query)")!
    
    requestRestriction(url: url,completionHandler: completionHandler)
}

func requestRestriction(url: URL,completionHandler: @escaping (Error?) -> Void) {
    let task = URLSession.shared.dataTask(with: url) { data,response,error in
        if let error = error {
            completionHandler(error)
            return
        }
        guard let httpResponse = response as? HTTPURLResponse,(200...299).contains(httpResponse.statusCode) else {
                completionHandler(RestrictError.failure)
                return
        }
        
        print("Restrict result",httpResponse)
        
        // Other than indicating success or failure,the `restrict` API doesn't return actionable data in its response.
        if data != nil {
            completionHandler(nil)
        } else {
            completionHandler(RestrictError.failure)
        }
    }
    task.resume()
}

/// A utility function that percent encodes a token for URL requests.
func encodetoken(_ token: String) -> String {
    return token.addingPercentEncoding(
        withAllowedCharacters: CharacterSet(charactersIn: "+/=").inverted
    ) ?? token
}

/// An error type that represents a failure in the `restrict` API call.
enum RestrictError: Error {
    case failure
}

func createString() {
    let record = CKRecord(recordtype: "TestString")
    
    record["testString"] = "Hello"
    container.publiccloudDatabase.save(record) { record,error in
        if error != nil {
            print("?",error!)
        }else {
            print(record!)
        }
    }
}
func unrestrict(container: CKContainer,completionHandler: @escaping (Error?) -> Void) {
    let webToken = encodetoken(webToken)
    
    let identifier = container.containerIdentifier!
    let env = "development" // Use "development" during development.
    let baseURL = "https://api.apple-cloudkit.com/database/1/"
    let apiPath = "\(identifier)/\(env)/private/users/unrestrict"
    let query = "?ckAPIToken=\(apiToken)&ckWebAuthToken=\(webToken)"
    
    let url = URL(string: "\(baseURL)\(apiPath)\(query)")!
    print(url)
    requestRestriction(url: url,completionHandler: completionHandler)
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。