Get the difference between the Address book information in iOS8.0 and 9.0

Source: Internet
Author: User
Tags deprecated

This article focuses on the iOS8.0 and iOS9.0 have interface and no interface to obtain contacts information method. Let's go through the first step, we're going to make it clear that both iOS8.0 and iOS9.0 don't need authorization in the case of interfaces .

1.ios8.0 interface operation required import Addressbookui static library follows Abpeoplepickernavigationcontrollerdelegate protocol

It is important to note that we do not need to bridge the situation to pay attention to the memory leak problem

#import "ViewController.h"

#import <AddressBookUI/AddressBookUI.h>

@interface Viewcontroller () <ABPeoplePickerNavigationControllerDelegate>

@end

@implementation Viewcontroller

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event

{

Create a Controller

Eject Controller

Abpeoplepickernavigationcontroller *picker = [[Abpeoplepickernavigationcontroller alloc]init];

Set up Proxy

Picker.peoplepickerdelegate = self;

Display interface

[Self Presentviewcontroller:picker animated:yes completion:nil];

}

Select a person's information will be called

Parameter 1 Controller parameter 2 information for the person selected

-(void) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) Peoplepicker DidSelectPerson: ( ABRECORDREF) person

{

Goal: Get contact information

Corefountion

Pointer record records database field column record row

Requirements: Name phone number

The C language API feature implements any function that calls a function parameter return value input and output

Name

Cfstringref Strfirst = Abrecordcopyvalue (person, kabpersonfirstnameproperty);

Cfstringref strlast = Abrecordcopyvalue (person, kabpersonlastnameproperty);

NSString *STRF = (__bridge_transfer NSString *) (Strfirst);

NSString *strl = (__bridge_transfer NSString *) (strlast);

NSLog (@ "%@%@", Strf,strl);

Phone number abmultivalueref encapsulates a type that holds a lot of phone calls

Abmultivalueref multivalues = Abrecordcopyvalue (person, kabpersonphoneproperty);

function to do

Cfindex index = Abmultivaluegetcount (multivalues);

for (Cfindex i = 0; i < index; i++) {

Cfstringref Strphone = Abmultivaluecopyvalueatindex (multivalues, i);

NSString *phonestr = (__bridge_transfer NSString *) (Strphone);

NSLog (@ "%@", phonestr);

}

if (multivalues! = NULL) {

Cfrelease (multivalues);

}

Memory Static Analysis tool

C language API corefoundation create copy and other objects obtained need to be released---Cfrelease

Bridge connection

__bridge default Bridging

__bridge_transfer bridging Coref, OcF ownership of the transferred object

__bridge_retained Bridging OcF--coref ownership of the transferred object

}

/* Backup * * Pay attention to memory leaks without using bridging

-(void) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) Peoplepicker DidSelectPerson: ( ABRECORDREF) person

{

Goal: Get contact information

Corefountion

Pointer record records database field column record row

Requirements: Name phone number

The C language API feature implements any function that calls a function parameter return value input and output

Name

Cfstringref Strfirst = Abrecordcopyvalue (person, kabpersonfirstnameproperty);

Cfstringref strlast = Abrecordcopyvalue (person, kabpersonlastnameproperty);

NSString *STRF = (__bridge NSString *) (Strfirst);

NSString *strl = (__bridge NSString *) (strlast);

NSLog (@ "%@%@", Strf,strl);

if (strfirst! = NULL) {

Cfrelease (Strfirst);

}

if (strlast! = NULL)

{

Cfrelease (Strlast);

}

Phone number abmultivalueref encapsulates a type that holds a lot of phone calls

Abmultivalueref multivalues = Abrecordcopyvalue (person, kabpersonphoneproperty);

function to do

Cfindex index = Abmultivaluegetcount (multivalues);

for (Cfindex i = 0; i < index; i++) {

Cfstringref Strphone = Abmultivaluecopyvalueatindex (multivalues, i);

NSString *phonestr = (__bridge_transfer NSString *) (Strphone);

NSLog (@ "%@", phonestr);

if (strphone! = NULL) {

Cfrelease (Strphone);

}

}

if (multivalues! = NULL) {

Cfrelease (multivalues);

}

Memory Static Analysis tool

C language API corefoundation create copy and other objects obtained need to be released---Cfrelease

Bridge connection

__bridge default Bridging

__bridge_transfer bridging Coref, OcF ownership of the transferred object

__bridge_retained Bridging OcF--coref ownership of the transferred object

}

Select a person's information will be called must be a select attribute to call

Note that if you implement the above Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) Peoplepicker DidSelectPerson: ( ABRECORDREF) The person current method does not perform the interface does not display

-(void) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) Peoplepicker DidSelectPerson: ( ABRECORDREF) Person Property: (Abpropertyid) Property identifier: (Abmultivalueidentifier) identifier

{

NSLog (@ "%zd", property);

}

If you click Cancel, you will be called

-(void) Peoplepickernavigationcontrollerdidcancel: (Abpeoplepickernavigationcontroller *) PeoplePicker

{

NSLog (@ "Peoplepickernavigationcontrollerdidcancel");

}

2.iOS8.0 Interface-free operation requires authorization to import addressbook static library

#import "ViewController.h"

#import <AddressBook/AddressBook.h>

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Get the old knowledge of the user's authorization

/*

kabauthorizationstatusnotdetermined = 0,//deprecated, use cnauthorizationstatusnotdetermined

kabauthorizationstatusrestricted,//deprecated, use cnauthorizationstatusrestricted

kabauthorizationstatusdenied,//deprecated, use cnauthorizationstatusdenied

Kabauthorizationstatusauthorized//Authorized

*/

Abauthorizationstatus status = Abaddressbookgetauthorizationstatus ();

if (status = = kabauthorizationstatusnotdetermined) {

Parameter 1 Address Book object parameter 2block callback

Abaddressbookref book = abaddressbookcreatewithoptions (null, NULL);

Request for authorization

Abaddressbookrequestaccesswithcompletion (book, ^ (bool granted, cferrorref error) {

if (granted) {

NSLog (@ "authorized success!");

}else{

NSLog (@ "Authorization failed!");

}

});

}

}

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event

{

Get contact information

Name

Abaddressbookref book = abaddressbookcreatewithoptions (null, NULL);

Cfarrayref allpeople = abaddressbookcopyarrayofallpeople (book);

Cfindex count = Cfarraygetcount (allpeople);

for (Cfindex i = 0; I <count; i++) {

Abrecordref record = Cfarraygetvalueatindex (allpeople, i);

Cfstringref Strfirst = Abrecordcopyvalue (record, kabpersonfirstnameproperty);

NSString *str = (__bridge_transfer NSString *) Strfirst;

NSLog (@ "%@", str);

Phone number

Abmultivalueref multivalue = Abrecordcopyvalue (record, kabpersonphoneproperty);

for (Cfindex i = 0; i < Abmultivaluegetcount (multivalue); i++) {

Cfstringref phonestr = Abmultivaluecopyvalueatindex (multivalue, i);

NSString *phone = (__bridge_transfer NSString *) (PHONESTR);

NSLog (@ "%@", phone);

}

}

}

3.ios9.0 interface operation is required to import Contactsui static library

#import "ViewController.h"

#import <ContactsUI/ContactsUI.h>

@interface Viewcontroller () <CNContactPickerDelegate>

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

}

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event

{

No authorization required

1. Create Address Book Controller

Cncontactpickerviewcontroller *picker = [[Cncontactpickerviewcontroller alloc]init];

2. Set up the agent to get the data itself

Picker.delegate = self;

3. Display

[Self Presentviewcontroller:picker animated:yes completion:nil];

}

-(void) Contactpickerdidcancel: (Cncontactpickerviewcontroller *) picker;

{

NSLog (@ "Contactpickerdidcancel");

}

Select a contact information

-(void) Contactpicker: (Cncontactpickerviewcontroller *) Picker didselectcontact: (cncontact *) Contact;

{

NSLog (@ "didselectcontact");

NSLog (@ "%@", contact.givenname);

Nsarray *allarray = contact.phonenumbers;

For (Cnlabeledvalue * Labeledvalue in Allarray) {

Cnphonenumber *num = Labeledvalue.value;

NSLog (@ "num =%@", num.stringvalue);

NSLog (@ "===%@ ===%@", Labeledvalue.value,labeledvalue.label);

}

}

Select a Contact property specific information

-(void) Contactpicker: (Cncontactpickerviewcontroller *) Picker Didselectcontactproperty: (Cncontactproperty *) Contactproperty;

{

NSLog (@ "Didselectcontactproperty");

}

4.ios9.0 interface-free operation requires authorization to import Contacts static libraries

#import "ViewController.h"

#import <Contacts/Contacts.h>

@interface Viewcontroller ()

@property (nonatomic, strong) Cncontactstore *store;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Authorized

Cnauthorizationstatus status = [Cncontactstore authorizationstatusforentitytype:cnentitytypecontacts];

If you are not authorized to request a user's authorization

Cncontactstore *store = [[Cncontactstore alloc]init];

Self.store = store;

if (status = = cnauthorizationstatusnotdetermined) {

Request authorization

[Store Requestaccessforentitytype:cnentitytypecontacts completionhandler:^ (BOOL Granted, Nserror * _Nullable error) {

if (granted) {

NSLog (@ "authorized success!");

}else{

NSLog (@ "Authorization failed!");

}

}];

}

}

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event

{

Name Phone

Cncontactfetchrequest *request = [[Cncontactfetchrequest Alloc]initwithkeystofetch:@[cncontactgivennamekey, Cncontactphonenumberskey]];

Parameter 1 encapsulates a query request

[Self.store enumeratecontactswithfetchrequest:request error:nil usingblock:^ (cncontact * _Nonnull Contact, BOOL * _ Nonnull stop) {

The returned data cncontact

NSLog (@ "%@", contact.givenname);

NSLog (@ "%@", contact.phonenumbers);

}];

}

Get the difference between the Address book information in iOS8.0 and 9.0

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.