简体   繁体   English

在iOS Swift中使用我的应用程序查询用户(解析)

[英]Query users using my app in iOS swift (Parse)

In my application users stores their phone number. 用户在我的应用程序中存储他们的电话号码。 Using the phone number I did a query that compares the phone number from address book and the phone number stored in the database (parse back-end) for match users using the app. 使用电话号码,我进行了一个查询,将通讯录中的电话号码与数据库中存储的电话号码(解析后端)进行比较,以查找使用该应用程序的匹配用户。 The problem is that my table did not return any data. 问题是我的表没有返回任何数据。 I did the basic configuration to request address book and table delegate functions. 我进行了基本配置,以请求地址簿和表委托功能。 This is my query function: 这是我的查询功能:

import UIKit
import AddressBook
import Parse

class mainContactViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {


    @IBOutlet weak var namesTableView: UITableView!


    var addressBook: ABAddressBook!
    var names:[String]?

    func createAddressBook() {

        var error: Unmanaged<CFErrorRef>?

        addressBook = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue()

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.namesTableView.delegate = self
        self.namesTableView.dataSource = self

        accessAddressBook()

        self.namesTableView.allowsMultipleSelection = true

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func accessAddressBook () {
        switch ABAddressBookGetAuthorizationStatus(){
        case .Authorized:
            println("Already authorized")
            createAddressBook()
            beginContactSearch()
            /* Access the address book */
        case .Denied:
            println("Denied access to address book")

        case .NotDetermined:
            createAddressBook()
            if let theBook: ABAddressBookRef = addressBook{
                ABAddressBookRequestAccessWithCompletion(theBook,
                    {(granted: Bool, error: CFError!) in

                        if granted{
                            println("Access granted")
                        } else {
                            println("Access not granted")
                        }

                })
            }

        case .Restricted:
            println("Access restricted")

        default:
            println("Other Problem")
            }
        }


func beginContactSearch(){
   let people = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as   NSArray as [ABRecord]

            for person in people {
                // Name
                let name = ABRecordCopyCompositeName(person).takeRetainedValue()

                // Phones
                let phones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
                names = Array()
                for (var i = 0; i < ABMultiValueGetCount(phones); i++) {
                    let phones: String = ABMultiValueCopyValueAtIndex(phones, i).takeRetainedValue() as String
                    var friendsquery = PFQuery(className:"_Users")
                        friendsquery.whereKey("phoneNumber" , equalTo:phones)
                        friendsquery.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
                            if error == nil{
                              //  if let objects = objects as? [String] {
                                    for object in objects {
                                        var phoneslist = object as NSString
                                       self.names!.append(phoneslist)

                                    }
                                //}
                            }
                        }
                    }
                }
                    self.namesTableView.reloadData()
            }

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.names?.count ?? 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = namesTableView.dequeueReusableCellWithIdentifier("Tablecell") as UITableViewCell

        cell.textLabel!.text = names![indexPath.row]
        cell.textLabel?.textColor = UIColor.whiteColor()

        return cell
    }


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}

I´m sorry, I need to guess what is the error because its hard to find it without the project... :-) 很抱歉,我想猜出是什么错误,因为没有项目就很难找到它... :-)

try to exchange the code in your file with the code under this line 尝试与该行下的代码交换文件中的代码

friendsquery.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
   if error == nil{
      for object in objects! {
         if let phoneslist: NSString = object as? NSString {
            self.names!.append(phoneslist)
         }
      }
   }
}

UPDATE 更新

Ok, my first guess was right :-) So let me try another one... 好吧,我的第一个猜测是正确的:-)所以让我再尝试一个...

In this code: 在此代码中:

if let phoneslist: NSString = object as? NSString {
   self.names!.append(phoneslist)
}

You try to cast object as NSString... Well I don't know Parse and I don´t know how their objects are build, but you need to do something like this... 您尝试将对象转换为NSString ...好吧,我不了解Parse,也不知道它们的对象是如何构建的,但是您需要执行以下操作...

if let phoneslist: PFObject = object as? PFObject {
   if let nameFromList: NSString = phoneslist["name"] as? NSString {
       self.names!.append(nameFromList)
   }
}

I guess that PFObject is an object holding multiple values... So you have to also define which of that values you want by its key... 我想PFObject是一个拥有多个值的对象...所以您还必须通过其键定义想要的那个值...

So you need to NSLog object to get its keys, and after that you change the code to my new code above and you are done 因此,您需要使用NSLog对象获取其密钥,然后将代码更改为上面的新代码,然后完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM