Reputation: 51
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.
Hi everyone,
I need your help.
Fetching is not working. Also, this error is unresolved
Use of unresolved identifier 'errorHandler'
I have two texts that I want to fetch in my MasterViewController
The texts are:
@IBOutlet weak var userEmailAddressTextField: UITextField!
@IBOutlet weak var userPasswordTextField: UITextField!
MasterViewController Code: Please go to fetching section
// 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
....................... Changes After feedback
//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")
Upvotes: 0
Views: 998
Reputation: 51
The answer source for this question is in techotopia
look for >Searching for Cloud Database Records
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")
}
}
}
}))
}
Upvotes: 1
Reputation: 13127
The user will already be logged in into iCloud. You do not need an extra login.You can just get the id of the user and use that as the username.
If you do want to use this code, then you probably did not define the errorHandler function (its not in your sample code). Maybe you should first try this without the self.handleCallback. You an just remove that line (well, you should replace it for an if for the error) Did you use this structure for the .handleCallback? That structure works better if you keep your CloudKit code in a separate class and call those methods with a completion and error handler.
replace the line with the self.handleCallback with:
if error != nil {
Besides that you should also change your predicate. You are now querying all records. You want to limit it with a filter on the username.
And if you want to use the iCloud ID instead of your own login, then you could call this:
CKContainer.defaultContainer().fetchUserRecordIDWithCompletionHandler({recordID, error in
Upvotes: 0