我正在使用 cloudkit 在公共数据库中读取和写入 - 在我的应用程序用户中假设能够上传文件并将记录写入数据库和其他用户读取它我正在使用 cloudkit 但是据我所知写入公共数据库用户必须使用 icloud 帐户登录,但苹果不允许在生产中这样做,所以如何解决这个问题 - 我如何赋予用户写入 DB 的权利
let Container = CKContainer.default()
let database = Container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "NewCode", predicate: predicate) //Photos is table name in cloudkit server
//-------Fetch the data---------------------
database.perform(query, inZoneWith: nil,
completionHandler: ({results, error in
DispatchQueue.main.async() { //Disp
if (error != nil) {
DispatchQueue.main.async() {
}
}
else { //a
if results!.count > 0 {
print("count = \(results!.count)")
record = results![0]
currentRecord = record
newcode = currentRecord?.object(forKey: "Code") as! String
newcodevalue = Int(newcode)!
newcodevalue = newcodevalue + 10
print("new code is = \(newcodevalue)")
myCodeStrinValue = String(newcodevalue)
print("new code string = \(newcodevalue)")
record?.setObject(myCodeStrinValue as CKRecordValue?,forKey: "Code")
database.save(record!, completionHandler: //save
({returnRecord, error in
if let err = error {
DispatchQueue.main.async() {
}
} else {
DispatchQueue.main.async() {
print("NewCode Table updated successfully")
// passing the code value we fetched above to the second viewcontroller "Uploadphotoviewcontroller" to be saved with the uploaded photo when saved button on the second controller clicked. remember you have to set an ID for the second view controller "Uploadphotoviewcontroller" to be used here when passing the value to it (set ID in the attributes right panel
// Instantiate SecondViewController
let UploadPhotoviewcontroller = self.storyboard?.instantiateViewController(withIdentifier:
"UploadPhotoviewcontroller") as! UploadPhotoviewcontroller
// Set the code value got from the DB above to the variable "myCodeValue" (this variable declared in the second view controller that will receive the value passed from here "Uploadphotoviewcontroller"
// add alpha numeric value to the code to make it more complicated for secuirty reasons
let CodeLetter = self.randomAlphaNumericString (length: 3)
UploadPhotoviewcontroller.myCodeValue = CodeLetter + myCodeStrinValue
UploadPhotoviewcontroller.codeOnly = myCodeStrinValue
// Take user to SecondViewController and accordingly remember not create graphical sague way link in the main storyboard to avoid reload of the view controller - remember to set ID attribute to UploadPhotoviewcontroller to use it here
self.navigationController?.pushViewController(UploadPhotoviewcontroller, animated: true)
}
}
})) //save
}
else { //G
DispatchQueue.main.async()
{
}
} //G
} //a
} //Disp
}))