【问题标题】:Retrieving texts Records in CloudKit with RecordID Using Swift使用 Swift 在 CloudKit 中使用 RecordID 检索文本记录
【发布时间】:2015-11-11 19:19:27
【问题描述】:

项目的目的

用户需要输入邮箱和密码才能登录。

CloudKit 用于检索用户凭据。

大家好,

我需要你的帮助。
提取不起作用。此外,此错误未解决 使用未解析的标识符“errorHandler”

我想在我的 MasterViewController 中获取两个文本

正文如下:

@IBOutlet weak var userEmailAddressTextField: UITextField!
@IBOutlet weak var userPasswordTextField: UITextField!

MasterViewController 代码: 请转到获取部分

// signIn btn
@IBAction func btnSignInTapped(sender: UIButton)
{
    let userEmailAddress = userEmailAddressTextField.text
    let userPassword = userPasswordTextField.text


    if(userEmailAddress!.isEmpty || userPassword!.isEmpty)
    {
        // Display an alert message
        let myAlert = UIAlertController(title: "Alert", message:"All fields are required to fill in", preferredStyle: UIAlertControllerStyle.Alert);
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:nil)
        myAlert.addAction(okAction);
        self.presentViewController(myAlert, animated: true, completion: nil)
        return
    }

    //************************Fetching Section
    //loading system indicator
    let accountID = CKRecordID!.self
    let database = CKContainer.defaultContainer().privateCloudDatabase
    var query = CKQuery(recordType:"RegisteredAccounts" , predicate: NSPredicate(value: true))
    var operation = CKQueryOperation(query: query)
    let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true)

    spinningActivity.labelText = "SigningIn"
    spinningActivity.detailsLabelText = "Please wait"

    operation.recordFetchedBlock = { record in /*is this your record...*/ }

    operation.queryCompletionBlock =
    { cursor, error in
        self.handleCallback(error, errorHandler: {errorHandler(error: error)}, completionHandler:
        {
            // ready fetching records
            if(userEmailAddress! == accountID || userPassword! == accountID)
            {
             //AlertMessage"You are loggedIn"
            }
            else{

                userEmailAddress! != accountID || userPassword! != accountID
                //AlertMessage"Your Credentials do not match"

            }


        })
    }

    operation.resultsLimit = CKQueryOperationMaximumResults;
    database.addOperation(operation)
    spinningActivity.hidden = true
}

Click here please for ScreenShot of the code

....................... 反馈后的变化

//loading system indicator
    let database = CKContainer.defaultContainer().privateCloudDatabase
    var query = CKQuery(recordType:"RegisteredAccounts" , predicate: NSPredicate(value: true))
    var operation = CKQueryOperation(query: query)

    //changes
    //****default_Login
    CKContainer.defaultContainer().fetchUserRecordIDWithCompletionHandler { (CKRecordID, error) in
        if error != nil
        {
            if(userEmailAddress! == CKRecordID || userPassword! == CKRecordID)
            {
                //self.spinningIndicator("Loggedin")
                self.alertMessage("LoggedIn")
            }

        }
        else{
            userEmailAddress! != CKRecordID || userPassword! != CKRecordID
            //self.spinningIndicator("Credentials don't match.")
            self.alertMessage("not matched")

        }
    }

    operation.recordFetchedBlock = { record in /*is this your record...*/ }
    operation.queryCompletionBlock =
    { cursor, error in
        if error != nil
        {
            print(error)
        }
        else{
            print(cursor)
        }

    }

    operation.resultsLimit = CKQueryOperationMaximumResults;
    database.addOperation(operation)
}

func spinningIndicator(userIndicator:String)
{
    let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
        spinningActivity.labelText=userIndicator
        spinningActivity.detailsLabelText = "Please wait"
        spinningActivity.hidden = true
}

func alertMessage(userAlert: String)
{
    // Display an alert message
    let myAlert = UIAlertController(title: "Alert", message:userAlert, preferredStyle: UIAlertControllerStyle.Alert);
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:nil)
    myAlert.addAction(okAction);
    self.presentViewController(myAlert, animated: true, completion: nil)
    return
}

我只是收到这条消息self.alertMessage("not matched")的提示

为什么我没有收到提示? self.alertMessage("LoggedIn")

【问题讨论】:

  • 我刚刚添加了代码截图

标签: swift cloudkit


【解决方案1】:

这个问题的答案来源 在techotopia

查找>搜索云数据库记录

答案在 performQuery 方法中

@IBAction func performQuery(sender: AnyObject) {

let predicate = NSPredicate(format: "address = %@", addressField.text)

let query = CKQuery(recordType: "Houses", predicate: predicate)

publicDatabase?.performQuery(query, inZoneWithID: nil, 
              completionHandler: ({results, error in

    if (error != nil) {
        dispatch_async(dispatch_get_main_queue()) {
            self.notifyUser("Cloud Access Error", 
                message: error.localizedDescription)
        }
    } else {
        if results.count > 0 {

            var record = results[0] as! CKRecord
            self.currentRecord = record

            dispatch_async(dispatch_get_main_queue()) {

                self.commentsField.text = 
                   record.objectForKey("comment") as! String

                let photo = 
                   record.objectForKey("photo") as! CKAsset

                let image = UIImage(contentsOfFile: 
                    photo.fileURL.path!)

                self.imageView.image = image
                self.photoURL = self.saveImageToFile(image!)
            }
        } else {
            dispatch_async(dispatch_get_main_queue()) {
                self.notifyUser("No Match Found", 
                 message: "No record matching the address was found")
            }
        }
    }
}))

}

【讨论】:

    【解决方案2】:

    用户已经登录到 iCloud。您不需要额外的登录。您只需获取用户的 id 并将其用作用户名。

    如果您确实想使用此代码,那么您可能没有定义 errorHandler 函数(它不在您的示例代码中)。也许您应该首先尝试不使用 self.handleCallback。您只需删除该行(好吧,您应该将其替换为错误的 if)您是否使用 this structure 来表示 .handleCallback?如果您将 CloudKit 代码保存在单独的类中并使用完成和错误处理程序调用这些方法,则该结构会更好地工作。

    用 self.handleCallback 替换该行:

    if error != nil {
    

    此外,您还应该更改谓词。您现在正在查询所有记录。您想通过用户名过滤器来限制它。

    如果你想使用 iCloud ID 而不是你自己的登录名,那么你可以这样称呼:

    CKContainer.defaultContainer().fetchUserRecordIDWithCompletionHandler({recordID, error in
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 2015-11-15
    • 1970-01-01
    相关资源
    最近更新 更多