IOS Get Contacts

Source: Internet
Author: User
Tags uikit

AddressBook.h
addressbook.h//yuaddressbook////Created by Yuzhx on 15/8/1.//Copyright (c) 2015 Bruceyu. All rights reserved.//#import <Foundation/Foundation.h> #import "AddressBookObj.h" @interface AddressBook: nsobject+ (addressbook*) sharedinstance;/** * Return Address Book Object * * @return (addressbookobj) **/+ (nsmutablearray*) addressbooks;/* * * To determine if there is phonenum * * @param phonenum Contact Phone * * @return (Nsmutablearray) **/+ (BOOL) Containphonenum: (nsstring*) phonenum;@ End


ADDRESSBOOK.M

  addressbook.m//  yuaddressbook////  created by yuzhx on  15/8/1.//  Copyright  (c)  2015 year  bruceyu. all rights reserved.//# import <addressbook/addressbook.h> #import  <UIKit/UIKit.h> #import   "AddressBook.h" # Define issafestring (a)               ((a) &&  (![ (a)  isequal:[nsnull null])  && ((a). length>0)) #define  safestring (a)                  ((((a) ==nil) | | ([(a)  isequal:[nsnull null]]) | | ((a). length==0)) [email protected] "":(a)) static addressbook *sharedaddressbook;static dispatch_once_t  Oncetoken, @interface  addressbook () @property   (assign,nonatomic) abaddressbookref addressbooksref; @property   (strong,nonatomic) nsmutablearray *addressbooksarr;@end@implementation addressbook+ (addressbook*) sharedinstance{    dispatch_once (& Oncetoken, ^{        sharedaddressbook = [[addressbook  alloc]init];    });     return sharedaddressbook;} -  (instancetype) init{    self = [super init];     if  (self)  {    }    return self;} #pragma  mark --(abaddressbookref) addressbooksref{        if   (!_ADDRESSBOOKSREF)  {                 if  ([[uidevice currentdevice].systemversion floatvalue] >= 6.0]         {             _addressbooksref =  abAddressbookcreatewithoptions (null, null);                         dispatch_semaphore_t  Sema = dispatch_semaphore_create (0);             abaddressbookrequestaccesswithcompletion (_addressbooksref, ^ (bool granted,  Cferrorref error) {dispatch_semaphore_signal (SEMA);});             dispatch_semaphore_wait (sema,  Dispatch_time_forever);                     }else{                         _addressBooksRef =  Abaddressbookcreatewithoptions (_addressbooksref, nil);        }    }    return _addressbooksref;} -(nsmutablearray *) addressbooksarr{        if  (!_ Addressbooksarr)  {                 _addressBooksArr = [NSMutableArray array];                 NSArray *contacts =  (__bridge  nsarray *) abaddressbookcopyarrayofallpeople ([addressbook sharedinstance].addressbooksref);                 NSInteger  contactscount = [contacts count];                 for (int i = 0; i < contactscount; i+ +)         {            abrecordref record =  (__ BRIDGE&NBSP;ABRECORDREF) [contacts objectatindex:i];                         AddressBookObj  * addressbookobj = [[addressbookobj alloc] init];                         // Get the id            addressbookobj.recordid = of the contact person   (int) Abrecordgetrecordid (record);                         //Full Name              CFStringRef compositeNameRef =  Abrecordcopycompositename (Record);             addressbookobj.compositename =  safestring ((__bridge nsstring *) compositenameref);             compositenameref != null ? cfrelease (COMPOSITENAMEREF)   : null;                                                  //Handling Contact Phone numbers              ABMultiValueRef  phones =  Abrecordcopyvalue (Record, kabpersonphoneproperty);             for (Int i = 0; i < abmultivaluegetcount (phones);  i++)             {                 cfstringref phonelabelref = abmultivaluecopylabelatindex ( Phones, i);                 cfstringref localizedphonelabelref = abaddressbookcopylocalizedlabel (PHONELABELREF);                 cfstringref  phonenumberref = abmultivaluecopyvalueatindex (phones, i);                                  NSString * localizedPhoneLabel =  (__ bridge nsstring *)  localizedPhoneLabelRef;                 nsstring * phonenumber =  (__bridge nsstring *) phoneNumberRef;                                                   nsstring *phone = [ phonenumber stringbyreplacingoccurrencesofstring:@ "[^0-9]"  withstring:@ ""  options: Nsregularexpressionsearch range:nsmakerange (0, [phonenumber length])];                                  if  (i == 0)  {                      Addressbookobj.pbone = safestring (Phone);                }                 [ addressbookobj.phoneinfo setvalue:localizedphonelabel forkey:phone];                                  //Release                 phoneLabelRef == NULL ? :  Cfrelease (PHONELABELREF);                 localizedphonelabelref == null ? : cfrelease (LOCALIZEDPHONELABELREF);                 phonenumberref  == null ? : cfrelease (PhoneNUMBERREF);            }             if (phones != null)  cfrelease (phones);                                      if  (Issafestring (addressbookobj.pbone))  {                 [_addressBooksArr addObject:addressBookObj];             }                         cfrelease (record);         }    }         Return _addressbooKsarr;} + (nsmutablearray*) addressbooks{    return [addressbook sharedinstance]. Addressbooksarr;} + (BOOL) Containphonenum: (nsstring*) phonenum{        for  ( Addressbookobj *obj in [addressbook sharedinstance].addressbooksarr)  {                 if  (obj.phoneinfo[ Phonenum])  {                         return YES;         }    }    return no;} @end


Https://github.com/6357/YUAddressBook

IOS Get Contacts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.