Introduction to iOS Contacts & Third-Party Frameworks

First of all, I haven't updated my blog for a long time. First, I apologize to my classmates! So today's topic is about how to access the system address book and an introduction to a third-party address book framework, so don't talk nonsense! Go directly to the code!


In iOS , there are 2 frameworks that can access the user's address book

 AddressBookUI.framework

 Provides contact list interface, contact details interface, add contact interface, etc.

 Generally used to select contacts

 

 AddressBook.framework

 Pure C language API, only get contact data

 There is no UI interface display, you need to build your own contact display interface

 Most of the data types in it are based on the Core Foundation framework, which is extremely painful to use.

 

 Since iOS6 , the user's authorization is required to access the address book, so before using it, check whether the user has authorized (AddressBook.framework)

 Contacts are read directly from iOS8 by default



//

// ZZViewController.h

// 01 - private address book ( 1 )

//

//  Created by 周昭 on 2017/3/8.

//  Copyright © 2017 YYSP. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ZZViewController : UIViewController


@end


//

//  ZZViewController.m

// 01 - private address book ( 1 )

//

//  Created by 周昭 on 2017/3/8.

//  Copyright © 2017 YYSP. All rights reserved.

//


#import "ZZViewController.h"

#import <AddressBookUI/AddressBookUI.h>


@interfaceZZViewController ()<ABPeoplePickerNavigationControllerDelegate>


@end


@implementation ZZViewController


/*

 // The third-party framework RHAddressBook ( the third-party framework of the address book ) is more object-oriented

 // include/ recursive recursive search


 */

- (void)viewDidLoad {

    [super viewDidLoad];

    // 1.授权

//    [self getStatus];

}


- (void)getStatus

{

#warning - 这个不应该在某个控制器的时候获取授权应该在应用打开的时候就获取写在AppDelegate

    

/*

 ABAddressBookGetAuthorizationStatus() 授权状态

{

    kABAuthorizationStatusNotDetermined = 0, // 未决定

    kABAuthorizationStatusRestricted, // 特殊原因不能访问用户的通讯录

    kABAuthorizationStatusDenied, // 拒绝访问

    kABAuthorizationStatusAuthorized // 已经授权

}

*/

    // 1.0 获取用户的授权状态

    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

    

    // 2.0 如果授权是未决定的,则请求授权

    //    if (status == kABAuthorizationStatusNotDetermined) {

    //        // 2.1 获取通讯录对象

    //        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    //

    //        // 2.2 请求授权

    //        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

    //            if (granted) {

    //                NSLog(@"用户授权成功");

    //            } else {

    //                NSLog(@"用户授权失败");

    //            }

    //        });

    //    }

    

    // 上面的代码转换成一句话如果授权不成功直接返回

    if (status != kABAuthorizationStatusNotDetermined) return;

    //

    // 授权成功继续

}


/**

 *  点击界面跳转带UI的通讯录界面

 */

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];

    

    ppnc.peoplePickerDelegate = self;

    

    [self presentViewController:ppnc animated:YES completion:nil];

}


#pragma mark - ABPeoplePickerNavigationControllerDelegate代理方法

/**

 *  选中某一个联系人的时候会调用该方法

 *  @param person       选中的联系人

 */

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person

{

#pragma mark - 验证来了这个方法没有

    NSLog(@"%s", __func__);

    

#warning - 这里极度需要同学们注意

/*

 1. CoreFoundation --> Foundation 桥接(以及内存泄口一定要注意)

 2. Product --> Profile --> instrument --> leeks 检测内存泄漏

 */

    

    // 1.获取联系人姓名

    CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);

    CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);

    

    /*

     __bridge NSString * : 将对象所有权给Foundation框架一份,但是本身没有释放

     __bridge_transfer NSString * : 将对象所有权交给Foundation框架,并且自身释放掉对象

     CFBridgingRelease 一种写法一种方式

     copy\creat 的都需要释放

     方法过期他会实现2个方法 走俩个方法

     */

    NSString *first = (__bridge_transfer NSString *)(firstName);

    NSString *last = (__bridge NSString *)(lastName);

    

    NSLog(@"%@---%@",first, last);

    CFRelease(lastName);

    

    // 2.获取联系人电话

    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);

    CFIndex phoneCount = ABMultiValueGetCount(phones);

    

    for (CFIndex i = 0; i < phoneCount; i++) {

        NSString *phoneLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i));

        NSString *phoneValue = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));

        NSLog(@"%@ -- %@", phoneLabel, phoneValue);

    }

    

    // 3.释放对象

    CFRelease(phones);

}


#warning - 如果想实现了上面的方法那么下面的方法就不会走这一点要注意(同学们自己实践)

/**

 *  选中某一个联系人的某一个属性的时候会调用该方法

 *  @param person       选中的联系人

 *  @param property     选中的属性

 *  @param identifier   属性对应的标识

 */

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

{

    NSLog(@"%s",__func__);

}


/*

#pragma mark - iOS7的方法那么注意适配的问题

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

{

    return NO;

}


- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

{

    return NO;

}


#warning - iOS7要实现点击取消这个方法否则会崩掉

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

{

    [peoplePicker dismissViewControllerAnimated:YES completion:nil];

}

*/



@end





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325765049&siteId=291194637