使用 Swift 使用 RecordID 检索 CloudKit 中的文本记录
Retrieving texts Records in CloudKit with RecordID Using Swift
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")
用户将已经登录到 iCloud。您不需要额外的 login.You 就可以获取用户的 ID 并将其用作用户名。
如果您确实想使用此代码,那么您可能没有定义 errorHandler 函数(它不在您的示例代码中)。也许您应该先尝试不使用 self.handleCallback。您只需删除该行(好吧,您应该将其替换为错误的 if)您是否将 this structure 用于 .handleCallback?如果您将 CloudKit 代码保存在单独的 class 中并使用完成和错误处理程序调用这些方法,则该结构会更好。
将 self.handleCallback 行替换为:
if error != nil {
除此之外,您还应该更改谓词。您现在正在查询所有记录。你想用用户名的过滤器来限制它。
如果您想使用 iCloud ID 而不是您自己的登录名,那么您可以这样调用:
CKContainer.defaultContainer().fetchUserRecordIDWithCompletionHandler({recordID, error in
本题答案来源
在 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")
}
}
}
}))
}
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")
用户将已经登录到 iCloud。您不需要额外的 login.You 就可以获取用户的 ID 并将其用作用户名。
如果您确实想使用此代码,那么您可能没有定义 errorHandler 函数(它不在您的示例代码中)。也许您应该先尝试不使用 self.handleCallback。您只需删除该行(好吧,您应该将其替换为错误的 if)您是否将 this structure 用于 .handleCallback?如果您将 CloudKit 代码保存在单独的 class 中并使用完成和错误处理程序调用这些方法,则该结构会更好。
将 self.handleCallback 行替换为:
if error != nil {
除此之外,您还应该更改谓词。您现在正在查询所有记录。你想用用户名的过滤器来限制它。
如果您想使用 iCloud ID 而不是您自己的登录名,那么您可以这样调用:
CKContainer.defaultContainer().fetchUserRecordIDWithCompletionHandler({recordID, error in
本题答案来源 在 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")
}
}
}
}))
}