问题描述
我对我的 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 (将#修改为@)