Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. import UIKit
  2. import AddressBook
  3.  
  4. class ContactSelectTableViewController: UITableViewController
  5. {
  6. var contacts : NSArray = []
  7.  
  8. var emptyDictionary: CFDictionaryRef?
  9.  
  10. var addressBook: ABAddressBookRef?
  11.  
  12. override func viewDidLoad()
  13. {
  14. super.viewDidLoad()
  15.  
  16. // Uncomment the following line to preserve selection between presentations
  17. // self.clearsSelectionOnViewWillAppear = false
  18.  
  19. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  20. // self.navigationItem.rightBarButtonItem = self.editButtonItem()
  21.  
  22. checkContact()
  23. }
  24.  
  25. override func didReceiveMemoryWarning()
  26. {
  27. super.didReceiveMemoryWarning()
  28. // Dispose of any resources that can be recreated.
  29. }
  30.  
  31. // MARK: - Table view data source
  32.  
  33. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  34. // #warning Potentially incomplete method implementation.
  35. // Return the number of sections.
  36. return 1
  37. }
  38.  
  39. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  40. // #warning Incomplete method implementation.
  41. // Return the number of rows in the section.
  42. return 1
  43. }
  44.  
  45.  
  46. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  47. {
  48. let cell : ContactTableViewCell = tableView.dequeueReusableCellWithIdentifier("Contact Cell", forIndexPath: indexPath) as ContactTableViewCell
  49.  
  50. // Configure the cell...
  51.  
  52. //Setup Image
  53. cell.userImage.layer.cornerRadius = cell.userImage.frame.size.height/2
  54. cell.userImage.layer.borderColor = UIColor.blackColor().CGColor
  55. cell.userImage.layer.borderWidth = 0.5
  56. cell.userImage.layer.masksToBounds = true
  57. cell.userImage.clipsToBounds = true
  58.  
  59. return cell
  60. }
  61.  
  62. override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
  63. {
  64. tableView.deselectRowAtIndexPath(indexPath, animated: true)
  65. }
  66.  
  67. func extractABAddressBookRef(abRef: Unmanaged<ABAddressBookRef>!) -> ABAddressBookRef?
  68. {
  69. if let ab = abRef {
  70. return Unmanaged<NSObject>.fromOpaque(ab.toOpaque()).takeUnretainedValue()
  71. }
  72. return nil
  73. }
  74.  
  75. func checkContact()
  76. {
  77. if (ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.NotDetermined)
  78. {
  79. println("requesting access...")
  80. var errorRef: Unmanaged<CFError>? = nil
  81. addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
  82. ABAddressBookRequestAccessWithCompletion(addressBook, { success, error in
  83. if success {
  84. self.getContactNames()
  85. } else {
  86. 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)
  87.  
  88. let dismiss = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
  89.  
  90. // let Text = UIAlertAction(title: "Text", style: .Default, handler: nil) essentially a dismiss button
  91.  
  92. alertController.addAction(dismiss)
  93. self.presentViewController(alertController, animated: true, completion: nil)
  94. }
  95. })
  96. }
  97. else if (ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.Denied || ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.Restricted) {
  98. println("access denied")
  99. }
  100. else if (ABAddressBookGetAuthorizationStatus() == ABAuthorizationStatus.Authorized) {
  101. println("access granted")
  102. self.getContactNames()
  103. }
  104. }
  105.  
  106. func getContactNames()
  107. {
  108. var errorRef: Unmanaged<CFError>?
  109. addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
  110. var contactList: NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
  111. println("records in the array \(contactList.count)")
  112.  
  113. for record:ABRecordRef in contactList
  114. {
  115. var contactPerson: ABRecordRef = record
  116. var contactName: String = ABRecordCopyCompositeName(contactPerson).takeRetainedValue() as String
  117. println ("\(contactName)")
  118. }
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement