Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import AddressBook
- class ContactSelectTableViewController: UITableViewController
- {
- var contacts : NSArray = []
- var emptyDictionary: CFDictionaryRef?
- var addressBook: ABAddressBookRef?
- override func viewDidLoad()
- {
- super.viewDidLoad()
- // Uncomment the following line to preserve selection between presentations
- // self.clearsSelectionOnViewWillAppear = false
- // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
- // self.navigationItem.rightBarButtonItem = self.editButtonItem()
- checkContact()
- }
- override func didReceiveMemoryWarning()
- {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- // MARK: - Table view data source
- override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
- // #warning Potentially incomplete method implementation.
- // Return the number of sections.
- return 1
- }
- override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // #warning Incomplete method implementation.
- // Return the number of rows in the section.
- return 1
- }
- override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
- {
- let cell : ContactTableViewCell = tableView.dequeueReusableCellWithIdentifier("Contact Cell", forIndexPath: indexPath) as ContactTableViewCell
- // Configure the cell...
- //Setup Image
- cell.userImage.layer.cornerRadius = cell.userImage.frame.size.height/2
- cell.userImage.layer.borderColor = UIColor.blackColor().CGColor
- cell.userImage.layer.borderWidth = 0.5
- cell.userImage.layer.masksToBounds = true
- cell.userImage.clipsToBounds = true
- return cell
- }
- override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
- {
- tableView.deselectRowAtIndexPath(indexPath, animated: true)
- }
- func extractABAddressBookRef(abRef: Unmanaged<ABAddressBookRef>!) -> ABAddressBookRef?
- {
- if let ab = abRef {
- return Unmanaged<NSObject>.fromOpaque(ab.toOpaque()).takeUnretainedValue()
- }
- return nil
- }
- func checkContact()
- {
- if (ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.NotDetermined)
- {
- println("requesting access...")
- var errorRef: Unmanaged<CFError>? = nil
- addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
- ABAddressBookRequestAccessWithCompletion(addressBook, { success, error in
- if success {
- self.getContactNames()
- } else {
- let alertController = UIAlertController(title: nil , message: "If you want to allow 'PayMeBack' to acces your Contacts at a later time, you can do so through Privacy Settings.", preferredStyle: .Alert)
- let dismiss = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
- // let Text = UIAlertAction(title: "Text", style: .Default, handler: nil) essentially a dismiss button
- alertController.addAction(dismiss)
- self.presentViewController(alertController, animated: true, completion: nil)
- }
- })
- }
- else if (ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.Denied || ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.Restricted) {
- println("access denied")
- }
- else if (ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.Authorized) {
- println("access granted")
- self.getContactNames()
- }
- }
- func getContactNames()
- {
- var errorRef: Unmanaged<CFError>?
- addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
- var contactList: NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
- println("records in the array \(contactList.count)")
- for record:ABRecordRef in contactList
- {
- var contactPerson: ABRecordRef = record
- var contactName: String = ABRecordCopyCompositeName(contactPerson).takeRetainedValue() as String
- println ("\(contactName)")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement