Pick up your iphone system contacts

Source: Internet
Author: User

Here is a brief introduction to the coarse filter, the project needs to create its own, as long as the corresponding following statements can be


First we will add a library in the project Addressbook.framework

Then we'll create a model class:

The class name here in lowercase, too lazy to change, we must change the use of

#import <Foundation/Foundation.h>

@interface Model:nsobject
@property (nonatomic,assign) Nsinteger sectionnumber;
@property (nonatomic,assign) Nsinteger RecordID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *tel;


@end


The code in the specific Controllerview is written as follows:

Create a Txltableviewcontroller class


#import "TXLTableViewController.h"

#import <AddressBook/AddressBook.h>

#import "Model.h"
@interface Txltableviewcontroller ()
@property (Nonatomic,strong) Nsmutablearray *addressbooktemp;
@end
Nsmutablearray *addressbooktemp;
@implementation Txltableviewcontroller


-(void) Viewdidload {
[Super Viewdidload];
Uncomment the following line to preserve selection between presentations.
Self.clearsselectiononviewwillappear = NO;

Uncomment the following line to display a Edit button in the navigation bar for this view controller.
Self.navigationItem.rightBarButtonItem = Self.editbuttonitem;

Initialization
Self.addressbooktemp = [Nsmutablearray array];
Create a new Address Book
Abaddressbookref addressbooks = nil;

if ([[Uidevice currentdevice].systemversion Floatvalue] >= 6.0)

{
Addressbooks = abaddressbookcreatewithoptions (null, NULL);

Get Address Book permissions

dispatch_semaphore_t sema = dispatch_semaphore_create (0);

Abaddressbookrequestaccesswithcompletion (addressbooks, ^ (bool granted, cferrorref error) {dispatch_semaphore_signal (sema);});

Dispatch_semaphore_wait (Sema, dispatch_time_forever);
}
Else
{
Addressbooks = Abaddressbookcreate ();
}

Get everyone in the Address Book Abaddressbookcopyarrayofallpeople the return value is of type Cfarrayref
Cfarrayref allpeople = abaddressbookcopyarrayofallpeople (addressbooks);

Get the number of contacts in your address Book
Cfindex npeople = Abaddressbookgetpersoncount (addressbooks);

Loop to get everyone's personal information
for (Nsinteger i = 0; i < npeople; i++)
{
Create a new AddressBook model class
Model *address = [[Model alloc] init];
Get personal
Abrecordref person = cfarraygetvalueatindex (allpeople, i);
Get personal name Objective-c objects and Core Foundation objects are converted to and from each other, using __bridge
Cftyperef Name = Abrecordcopyvalue (person, kabpersonfirstnameproperty);
NSString *namestring = (__bridge NSString *) Name;
Get a personal phone
Abmultivalueref Tmlphone = Abrecordcopyvalue (person, kabpersonphoneproperty);
nsstring* Telphone = (__bridge nsstring*) abmultivaluecopyvalueatindex (tmlphone, 0);


Address.tel =telphone;
Address.name = namestring;
Read the unique identity of a contact in the Address Book
Address.recordid = (int) abrecordgetrecordid (person);
Add personal information to the array, and after the loop is complete, the addressbooktemp contains information about all the contacts
[Self.addressbooktemp addobject:address];


}
}

This article is from the iOS blog, so be sure to keep this source http://10136044.blog.51cto.com/10126044/1691389

Pick up your iphone system contacts

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.