ios(包括六、7)應用程序引用系統通信錄的方法 [親測可行]

     因爲ios系統對用戶隱私的控制,第三方應用程序只能經過蘋果官方接口調用系統通信錄,不能像android那樣直接操做通信錄數據庫。
     通常地,使用系統自帶通信錄的方法有兩種,一種是直接將整個通信錄引入到應用程序,另外一種是逐條讀取通信錄中的每一條聯繫人信息。下面咱們就一一詳解。android

1 直接引用整個通信錄ios

使用的類:ABPeoplePickerNavigationController
方法:數據庫

複製代碼

在LocalAddressBookController.h文件中 <UIKit/UIKit.h> <AddressBook/AddressBook.h> <AddressBookUI/AddressBookUI.h> LocalAddressBookController : UIViewController<ABPersonViewControllerDelegate,ABPeoplePickerNavigationControllerDelegate,ABNewPersonViewControllerDelegate>
{
    ABPeoplePickerNavigationController *picker;
    ABNewPersonViewController *personViewController;
}在LocalAddressBookController.m文件中   LocalAddressBookController

- ()initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     (self) {        
    }     self;
}

- ()didReceiveMemoryWarning
{    
    [super didReceiveMemoryWarning];    
    } mark - View lifecycle

- ()viewDidLoad
{
    [super viewDidLoad];
    
    picker = [[ABPeoplePickerNavigationController alloc]init];
    picker.view.frame = CGRectMake(, , Screen_width, Screen_height-);
    picker.peoplePickerDelegate = self;
    picker. = self;                   
    [picker setHidesBottomBarWhenPushed:YES];
    [picker setNavigationBarHidden:NO animated:NO];//顯示上方的NavigationBar和搜索框
    [self.view addSubview:picker.view];
} mark UINavigationControllerDelegate methods- ()navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    UIView *custom = [[UIView alloc] initWithFrame:CGRectMake(,,,)];
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom];
    [viewController.navigationItem setRightBarButtonItem:btn animated:NO];
    [btn release];
    [custom release];
} mark - peoplePickerDelegate Methods
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSLog(, (NSString*)ABRecordCopyCompositeName(person)); 
     YES;
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{    
     YES;
}
-(BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{     YES;
} -()peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{    
    [picker dismissModalViewControllerAnimated:YES];
}
//……

複製代碼

ios6 運行效果:app

ios7 運行效果:ide

 

經驗:url

當咱們將系統通信錄整個引入的時候,在通信錄的右上角有一個系統自帶的「取消」按鈕。如何才能將這個取消按鈕隱藏呢?spa

(1)方法1:若是你的應用程序是用企業證書開發,不須要提交到appStore進行審覈,那麼答案很是簡單,爲響應的piker增長以下代碼便可:code

[picker setAllowsCancel:NO];orm

(2)方法二:上面的方法是非公開方法,是沒法經過appStore審覈的。若是想經過審覈。能夠嘗試使用以下方法:接口

複製代碼

前提:設置 picker.delegate = self;
而後實現以下委託方法
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  UIView *custom = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f,0.0f,0.0f)]; 
  UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom]; 
  //UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)]; 
  [viewController.navigationItem setRightBarButtonItem:btn animated:NO]; 
  [btn release]; 
  [custom release]; 
}

複製代碼

或者:(比上面更好)

複製代碼

前提:設置 picker.delegate = self;
而後實現以下委託方法,下面實現的效果,要比上面的好。上面實現的效果,當點擊「搜索」框時,「取消」按鈕還會從新出現。
- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {    
    // Here we want to remove the 'Cancel' button, but only if we're showing    // either of the ABPeoplePickerNavigationController's top two controllers
    if ([navigationController.viewControllers indexOfObject:viewController] <= 1) {
        viewController.navigationItem.rightBarButtonItem = nil;
    }
}

複製代碼

 

二、逐條讀取通信錄中的每一條聯繫人信息。

方法:在上述類中,直接添加以下方法便可

複製代碼

在LocalAddressBookController.h文件中 <UIKit/UIKit.h> <AddressBook/AddressBook.h> <AddressBookUI/AddressBookUI.h> LocalAddressBookController : 
{
    UITextView *textView;
}在LocalAddressBookController.m文件中   LocalAddressBookController

- ()initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     (self) {        
    }     self;
}

- ()didReceiveMemoryWarning
{    
    [super didReceiveMemoryWarning];    
    } mark - View lifecycle

- ()viewDidLoad
{
    [super viewDidLoad];    
    textView = [[UITextView alloc]initWithFrame:CGRectMake(, , Screen_width, Screen_height)];
    [self getAddressBook];
    [self.view addSubview:textView];
}-()getAddressBook
{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    
    CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);    
    ( i = ; i < CFArrayGetCount(results); i++)
    {
        ABRecordRef person = CFArrayGetValueAtIndex(results, i);        
        NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);        (personName != nil)
            textView.text = [textView.text stringByAppendingFormat:,personName];        
        NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);        (lastname != nil)
            textView.text = [textView.text stringByAppendingFormat:,lastname];        
        NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);        (middlename != nil)
            textView.text = [textView.text stringByAppendingFormat:,middlename];        
        NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);        (prefix != nil)
            textView.text = [textView.text stringByAppendingFormat:,prefix];        
        NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);        (suffix != nil)
            textView.text = [textView.text stringByAppendingFormat:,suffix];        
        NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty);        (nickname != nil)
            textView.text = [textView.text stringByAppendingFormat:,nickname];        
        NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);        (firstnamePhonetic != nil)
            textView.text = [textView.text stringByAppendingFormat:,firstnamePhonetic];        
        NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);        (lastnamePhonetic != nil)
            textView.text = [textView.text stringByAppendingFormat:,lastnamePhonetic];        
        NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);        (middlenamePhonetic != nil)
            textView.text = [textView.text stringByAppendingFormat:,middlenamePhonetic];        
        NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);        (organization != nil)
            textView.text = [textView.text stringByAppendingFormat:,organization];        
        NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);        (jobtitle != nil)
            textView.text = [textView.text stringByAppendingFormat:,jobtitle];        
        NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);        (department != nil)
            textView.text = [textView.text stringByAppendingFormat:,department];        
        NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);        (birthday != nil)
            textView.text = [textView.text stringByAppendingFormat:,birthday];        
        NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty);        (note != nil)
            textView.text = [textView.text stringByAppendingFormat:,note];        
        NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);
        NSLog(,firstknow);        
        NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);
        NSLog(,lastknow);        
        
        ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);         emailcount = ABMultiValueGetCount(email);         ( x = ; x < emailcount; x++)
        {            
            NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));            
            NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, x);
            textView.text = [textView.text stringByAppendingFormat:,emailLabel,emailContent];
        }        
        ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);         count = ABMultiValueGetCount(address);        
        ( j = ; j < count; j++)
        {            
            NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j);
            textView.text = [textView.text stringByAppendingFormat:,addressLabel];            
            NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);
            NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];            (country != nil)
                textView.text = [textView.text stringByAppendingFormat:,country];
            NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];            (city != nil)
                textView.text = [textView.text stringByAppendingFormat:,city];
            NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];            (state != nil)
                textView.text = [textView.text stringByAppendingFormat:,state];
            NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];            (street != nil)
                textView.text = [textView.text stringByAppendingFormat:,street];
            NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];            (zip != nil)
                textView.text = [textView.text stringByAppendingFormat:,zip];
            NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];            (coutntrycode != nil)
                textView.text = [textView.text stringByAppendingFormat:,coutntrycode];
        }        
        
        ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);         datescount = ABMultiValueGetCount(dates);         ( y = ; y < datescount; y++)
        {            
            NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y));            
            NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, y);
            textView.text = [textView.text stringByAppendingFormat:,datesLabel,datesContent];
        }        
        CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);         (recordType == kABPersonKindOrganization) {            
            NSLog();
        }  {            
            NSLog();
        }        
        
        
        ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);         ( l = ; l < ABMultiValueGetCount(instantMessage); l++)
        {            
            NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);
            textView.text = [textView.text stringByAppendingFormat:,instantMessageLabel];            
            NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);
            NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];            (username != nil)
                textView.text = [textView.text stringByAppendingFormat:,username];
            
            NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];            (service != nil)
                textView.text = [textView.text stringByAppendingFormat:,service];
        }        
        
        ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);         ( k = ; k<ABMultiValueGetCount(phone); k++)
        {            
            NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));            
            NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k);
            
            textView.text = [textView.text stringByAppendingFormat:,personPhoneLabel,personPhone];
        }        
        
        ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);         ( m = ; m < ABMultiValueGetCount(url); m++)
        {            
            NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));            
            NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,m);
            
            textView.text = [textView.text stringByAppendingFormat:,urlLabel,urlContent];
        }        
        
        NSData *image = (NSData*)ABPersonCopyImageData(person);
        
        
        UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
        [myImage setImage:[UIImage imageWithData:image]];
        myImage.opaque = YES;
        [textView addSubview:myImage];
    }
    
    CFRelease(results);
    CFRelease(addressBook);
}
//……
@end

複製代碼

 

ios7運行效果:

相關文章
相關標籤/搜索