swift - 使用 Swift 使用 RecordID 检索 CloudKit 中的文本记录

标签 swift cloudkit

Purpose of the program

User need to enter the email and password in order to get logged in.

CloudKit is used to retrieve user credentials.

大家好,

我需要你的帮助。
抓取不起作用。另外,这个错误还没有解决 使用未解析的标识符“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
}

I am only prompted by this message self.alertMessage("not matched")

Why am I not prompted by this? self.alertMessage("LoggedIn")

最佳答案

该问题的答案来源 位于 techotopia

查找>搜索云数据库记录

The answer is in performQuery method

@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")
            }
        }
    }
}))

}

关于swift - 使用 Swift 使用 RecordID 检索 CloudKit 中的文本记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33521149/

相关文章:

swift - AVFoundation 连接视频和音频

ios - Reactive Cocoa - 然后是自定义信号与 UI 信号

ios - 交互式 UIView 翻转过渡

cloudkit - Xcode 14 RC : Not entitled to listen to push notifications. 突然出错 请添加 'aps-connection-initiate' 授权

objective-c - 使用 recordName 获取单个记录,它应该从 icloud 返回特定字段

UITableView 缓慢加载图像

ios - 带有谓词/排序函数闭包的 NSFetchRequest

ios - 在您的应用中启用CloudKit是否会影响Appstore中的当前应用?

swift - 匹配 Swift 中全文搜索的所有标记

ios - 如何将 Swift 中的 Core Data 与 Cloudkit 以及在许多设备上同步