2014-12-19 188 views
1

我試圖利用CloudKit在我的iPhone應用程序,但是當我試圖創建一個User記錄類型(記錄類型名爲Users)的新紀錄,我得到這個錯誤:創建操作不允許

<CKError 0x7fb80947ffb0: "Permission Failure" (10/2007); 
server message = "CREATE operation not permitted"; 
uuid = 8C4C7B60-E3F4-42FC-9551-D3A76A6FF9D6; 
container ID = "iCloud.com.jojodmo.Blix"> 

要創建新的記錄,我在viewDidLoad()使用此代碼:

UserCKManager.create(
    CloudKitUser(
     email: "[email protected]", password: "password", salt: "1AF4E759B20FEC32", 
     username: "jojodmo", posts: [], biography: "This is my bio", 
     firstName: "John", lastName: "Doe", flags: [], 
     profilePicture: UIImage(), downVotesGiven: 1, downVotesRecieved: 2, 
     upVotesGiven: 3, upVotesRecieved: 4, pictureProvided: false), callback: nil) 

這將創建一個新的CloudKitUser

init(email: String, password: String, salt: String, username: String, posts: [String], biography: String, firstName: String, lastName: String, flags: [String], profilePicture: UIImage, downVotesGiven: Int, downVotesRecieved: Int, upVotesGiven: Int, upVotesRecieved: Int, pictureProvided: Bool){ 
    self.ready = false 
    record = CKRecord(recordType: "Users") 
    self.email = email 
    self.password = password 
    self.salt = salt 
    self.username = username 
    self.posts = posts 
    self.biography = biography 
    self.firstName = firstName 
    self.lastName = lastName 
    self.flags = flags 
    self.profilePicture = profilePicture 
    self.downVotesGiven = downVotesGiven 
    self.downVotesRecieved = downVotesRecieved 
    self.upVotesGiven = upVotesGiven 
    self.upVotesRecieved = upVotesRecieved 
    self.pictureProvided = pictureProvided 
    self.ready = true 
} 

,然後嘗試創建一個記錄吧:

class func create(user: User, callback: ((success: Bool, user: User?) ->())?){ 
    let ckUser = CloudKitUser(user: user) 
    //I've also tried using CKContainer.defaultContainer().privateCloudDatabase, and I get the same error 
    CKContainer.defaultContainer().publicCloudDatabase.saveRecord(ckUser.record){(record, error) in 
     if(error != nil){ 
      println("An error occurred: \(error)") 
      callback?(success: false, user: nil) 
     } 
     else{ 
      println("Record saved successfully") 
      callback?(success: true, user: ckUser) 
     } 
    } 
} 

我知道這是不是與連接到數據庫的問題,因爲我已經得到了一個錯誤說,有沒有容器我給出的ID,我固定的。

我想在iOS模擬器上運行這個(我登錄到iCloud中就可以了)

+0

這似乎是一個權限問題。你可以直接用同一個用戶訪問數據庫嗎?如果是這樣,你可以創建你的記錄直接訪問?我相信第一個答案是肯定的,第二個答案是否定的。您需要爲數據庫用戶創建權限。 – 2014-12-19 06:22:37

+0

@LajosArpad第一個答案是肯定的,但第二個答案IS也不是,我怎樣才能給數據庫用戶創建權限?我覺得我會試圖,如果我知道如何 – Jojodmo 2014-12-19 06:36:30

回答

1

用戶是已經存在CloudKit一個特殊的記錄類型。你不應該自己創建記錄。記錄將自動爲正在使用您的應用的新用戶創建。 您也無法創建訂閱並查詢用戶記錄類型。您只能通過ID直接查詢用戶記錄。您可以將額外的數據存儲在用戶recordType中,但我認爲在您的情況下,如果您爲其他類型的記錄類型命名,則會更好。

+0

謝謝,有沒有辦法獲得currnet用戶的id,所以我可以查詢他們的密碼和用戶名? – Jojodmo 2014-12-19 15:44:29

+0

當前的iCloud用戶只有一個ID,名字和姓氏。您可以添加您自己的密碼字段,但爲什麼您的用戶需要登錄兩次?它已經陷入了iCloud。你可以使用容器上的fetchUserRecordIDWithCompletionHandler來獲得你的id。 – 2014-12-19 17:34:11

0

除了埃德溫的回答,這是完全可行的自定義字段保存到公共Users RECORDTYPE,但你需要(通過CKContainerfetchUserRecordID()discoverUserIdentity())來檢索CKRecordID用戶,然後再建立CKRecordUsers使用該ID。

此外,如果您使用的是CKModifyRecordsOperation,請確保savePolicy設置爲.changedKeys以允許更新通過。