使用Swift

时间:2015-11-04 11:51:17

标签: swift cloudkit

  

该计划的目的

     

用户需要输入电子邮件和密码才能登录。

     

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

2 个答案:

答案 0 :(得分: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")
            }
        }
    }
}))

}

答案 1 :(得分:0)

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

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

将self.handleCallback替换为:

if error != nil {

除此之外,您还应该更改谓词。您现在正在查询所有记录。您希望使用用户名上的过滤器来限制它。

如果你想使用iCloud ID代替自己的登录,那么你可以这样称呼:

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