2

Iam using cloudkit to read and write in public database - in my application user suppose to be able to upload files and write records to database and other users to read it I am using cloudkit however as far as I know to write to the public database user has to login with icloud account however apple does not allow this in production so how to solve this - how can I give the users a right to write to 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

    }))
rania
  • 57
  • 7
  • If the user is logged into their iCloud account on their device, that's all you really need. Other than that you want to make sure the permissions are setup properly. – Pierce Feb 06 '17 at 20:53
  • true but I have seen an application rejected by apple because it was asking the user to login to his icloud account - so what if he is not logged in – rania Feb 06 '17 at 20:58
  • 1
    If a user is not logged into an iCloud account, the user can't write to any CloudKit database. But they should be able to read from the public database. Adjust your UI as appropriate based on whether the user is logged in or not. Optionally, alert the user of the limited functionality due to not being logged in. But don't require a user to login. – rmaddy Feb 06 '17 at 21:17
  • @rmaddy you mean I can for example tell the user you can not use full functionalities if you re not logged in to your icloud account and leave him the choice to log or not?? and that way apple will accept it? – rania Feb 06 '17 at 22:59
  • Probably, if done right. Apple likes apps to work no matter what. They don't like when apps force people to login or create accounts when not needed. Make your app friendly as much as possible. – rmaddy Feb 06 '17 at 23:01
  • if you just allow read access to loggedbout users, you will be fine. – Simon Feb 07 '17 at 20:29

0 Answers0