Get Contact & Email ID From Iphone
1) ADD Framework :
- Contacts.Framework
- ContactsUI.Framework
2) Write Coding in ViewController.m :
#import "ViewController.h"
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES) {
//keys with fetching properties
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey,CNContactEmailAddressesKey];
NSString *containerId = store.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
} else {
NSMutableArray *marrContact=[[NSMutableArray alloc] init];
[marrContact removeAllObjects];
NSString *phone;
NSString *fullName;
NSString *firstName;
NSString *lastName;
NSString *emailAddress;
UIImage *profileImage;
NSMutableArray *contactNumbersArray=[[NSMutableArray alloc] init];
for (CNContact *contact in cnContacts) {
// copy data to my variable .
firstName = contact.givenName;
lastName = contact.familyName;
if (lastName == nil) {
fullName=[NSString stringWithFormat:@"%@",firstName];
}else if (firstName == nil){
fullName=[NSString stringWithFormat:@"%@",lastName];
}
else{
fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
}
UIImage *image = [UIImage imageWithData:contact.imageData];
if (image != nil) {
profileImage = image;
}else{
profileImage = [UIImage imageNamed:@"person-icon.png"];
}
for (CNLabeledValue *label in contact.phoneNumbers) {
phone = [label.value stringValue];
NSLog(@"%@",phone);
if ([phone length] > 0) {
[contactNumbersArray addObject:phone];
}
NSLog(@"Phone :%@",contactNumbersArray);
}
for (CNLabeledValue *label in contact.emailAddresses) {
emailAddress = label.value;
if ([emailAddress length] > 0) {
NSLog(@"%@",emailAddress);
// [emailArray addObject:email];
}
else
{
emailAddress=@"";
}
}
NSLog(@"%@",emailAddress);
NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: firstName,@"firstname",lastName,@"lastname",phone,@"PhoneNumbers",emailAddress,@"email", nil];
NSLog(@"%@",personDict);
[marrContact addObject:personDict];
NSLog(@"contact %@",marrContact);
phone=@"";
emailAddress=@"";
fullName=@"";
}
NSLog(@"all contact %@",marrContact);
dispatch_async(dispatch_get_main_queue(), ^
{
NSLog(@"%@",marrContact);
if (marrContact.count>0) {
NSMutableArray *marrList=[[NSMutableArray alloc] init];
[marrList removeAllObjects];
for (int i=0; i<marrContact.count; i++) {
if ([[[marrContact objectAtIndex:i] valueForKey:@"email"] length]>0) {
[marrList addObject:[marrContact objectAtIndex:i]];
}
}
if (marrList.count>0) {
// Delete record
}
// if (marrList.count>0) {
// marrCheck=[[NSMutableArray alloc] init];
// [marrCheck removeAllObjects];
// for (int i=0; i<marrList.count; i++) {
// [marrCheck addObject:@"N"];
// }
// [tblList reloadData];
// }
}
else{
NSLog(@"No Contact found");
}
});
}
}
}];
}
No comments:
Post a Comment