i working on existing objective c project , while reading address book ui framework reference ios found below classes have deprecated in ios 9 . ( abunknownpersonviewcontroller
, abpersonviewcontroller
, abpeoplepickernavigationcontroller
, abnewpersonviewcontroller
) replacement of .? can find document related . appreciated . in advance .
the addressbookui
framework has been deprecated in ios 9
, better should use contactsui
framework.
it has many new features including features of addressbookui
framework.
so, in case if targeting ios 9
should go contactsui
framework.
to check addressbookui
framework available specific ios
version can following:
if ([cncontactstore class]) { cncontactstore *store = [cncontactstore new]; //... } else { // fallback old framework }
here complete code that:
- (void) contactscan { if ([cncontactstore class]) { //ios9 or later cnentitytype entitytype = cnentitytypecontacts; if( [cncontactstore authorizationstatusforentitytype:entitytype] == cnauthorizationstatusnotdetermined) { cncontactstore * contactstore = [[cncontactstore alloc] init]; [contactstore requestaccessforentitytype:entitytype completionhandler:^(bool granted, nserror * _nullable error) { if(granted){ [self getallcontact]; } }]; } else if( [cncontactstore authorizationstatusforentitytype:entitytype]== cnauthorizationstatusauthorized) { [self getallcontact]; } } } -(void)getallcontact { if([cncontactstore class]) { //ios 9 or later nserror* contacterror; cncontactstore* addressbook = [[cncontactstore alloc]init]; [addressbook containersmatchingpredicate:[cncontainer predicateforcontainerswithidentifiers: @[addressbook.defaultcontaineridentifier]] error:&contacterror]; nsarray * keystofetch =@[cncontactemailaddresseskey, cncontactphonenumberskey, cncontactfamilynamekey, cncontactgivennamekey, cncontactpostaladdresseskey]; cncontactfetchrequest * request = [[cncontactfetchrequest alloc]initwithkeystofetch:keystofetch]; bool success = [addressbook enumeratecontactswithfetchrequest:request error:&contacterror usingblock:^(cncontact * __nonnull contact, bool * __nonnull stop){ [self parsecontactwithcontact:contact]; }]; } } - (void)parsecontactwithcontact :(cncontact* )contact { nsstring * firstname = contact.givenname; nsstring * lastname = contact.familyname; nsstring * phone = [[contact.phonenumbers valueforkey:@"value"] valueforkey:@"digits"]; nsstrubg * email = [contact.emailaddresses valueforkey:@"value"]; nsarray * addrarr = [self parseaddresswithcontac:contact]; } - (nsmutablearray *)parseaddresswithcontac: (cncontact *)contact { nsmutablearray * addrarr = [[nsmutablearray alloc]init]; cnpostaladdressformatter * formatter = [[cnpostaladdressformatter alloc]init]; nsarray * addresses = (nsarray*)[contact.postaladdresses valueforkey:@"value"]; if (addresses.count > 0) { (cnpostaladdress* address in addresses) { [addrarr addobject:[formatter stringfrompostaladdress:address]]; } } return addrarr; }
just make sure ask permission read contacts device.
reference link: https://gist.github.com/willthink/024f1394474e70904728
updated:
for replacement addressbookui
need use cncontactpickerviewcontroller
. can check delegate methods can used pickup 1 or multiple contacts @ time.
this present inbuilt uiviewcontroller
contacts , need implement delegate methods of it!
to select 1 contact:
contactpicker:didselectcontact:
to select multiple (new feature):
contactpicker:didselectcontacts:
cncontactpickerdelegate
reference: https://developer.apple.com/library/ios/documentation/contactsui/reference/cncontactpickerdelegate_protocol/
Comments
Post a Comment