iPhone : Insert Contact to Address book without an

2019-02-19 04:29发布

This question already has an answer here:

I am working on Address book and Contact related functionalities. In that I want to add contacts to my device without any user interaction. Using ABPerson class we are getting some user interface but my concern is

I do have contacts on my own server and I want to copy all from the server to my iPhone. So If there I will use a user interface then it will consume a lots of time.

So can any one help me out from this situation

Thanks in advance,

1条回答
女痞
2楼-- · 2019-02-19 05:26

complete answer to add contact to address book add the addressbook framework AddressBook.framework and AddressBookUI.framework

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/ABPerson.h>



- (void)viewDidLoad {
  ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

  if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)  
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
        // First time access has been granted, add the contact
         [self addContact];
    });
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // The user has previously given access, add the contact
     [self addContact];
}
else {
    // The user has previously denied access
    // Send an alert telling user to change privacy setting in settings app
}

}

 - (void)addContact {  
// Creating new entry  
 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); 
ABRecordRef person = ABPersonCreate();  

// Setting basic properties  
ABRecordSetValue(person, kABPersonFirstNameProperty, @"Ondrej" , nil);  
ABRecordSetValue(person, kABPersonLastNameProperty, @"Rafaj", nil);  
ABRecordSetValue(person, kABPersonJobTitleProperty, @"Tech. director", nil);  
ABRecordSetValue(person, kABPersonDepartmentProperty, @"iPhone development department", nil);  
ABRecordSetValue(person, kABPersonOrganizationProperty, @"Fuerte international", nil);  
ABRecordSetValue(person, kABPersonNoteProperty, @"The best iPhone development studio in the UK :)", nil);  

// Adding phone numbers  
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);  
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"07972574949", (CFStringRef)@"iPhone", NULL);  
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"01234567890", (CFStringRef)@"Work", NULL);  
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"08701234567", (CFStringRef)@"0870", NULL);  
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);  
CFRelease(phoneNumberMultiValue);  

// Adding url  
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);  
ABMultiValueAddValueAndLabel(urlMultiValue, @"http://www.fuerteint.com", kABPersonHomePageLabel, NULL);  
ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);  
CFRelease(urlMultiValue);  

// Adding emails  
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);  
ABMultiValueAddValueAndLabel(emailMultiValue, @"info@fuerteint.com", (CFStringRef)@"Global", NULL);  
ABMultiValueAddValueAndLabel(emailMultiValue, @"ondrej.rafaj@fuerteint.com", (CFStringRef)@"Work", NULL);  
ABRecordSetValue(person, kABPersonURLProperty, emailMultiValue, nil);  
CFRelease(emailMultiValue);  

// Adding address  
ABMutableMultiValueRef addressMultipleValue = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);  
NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];  
[addressDictionary setObject:@"8-15 Dereham Place" forKey:(NSString *)kABPersonAddressStreetKey];  
[addressDictionary setObject:@"London" forKey:(NSString *)kABPersonAddressCityKey];  
[addressDictionary setObject:@"EC2A 3HJ" forKey:(NSString *)kABPersonAddressZIPKey];  
[addressDictionary setObject:@"United Kingdom" forKey:(NSString *)kABPersonAddressCountryKey];  
[addressDictionary setObject:@"gb" forKey:(NSString *)kABPersonAddressCountryCodeKey];  
ABMultiValueAddValueAndLabel(addressMultipleValue, addressDictionary, kABHomeLabel, NULL);  
[addressDictionary release];  
ABRecordSetValue(person, kABPersonAddressProperty, addressMultipleValue, nil);  
CFRelease(addressMultipleValue);  

// Adding person to the address book  
ABAddressBookAddRecord(addressBook, person, nil);  
CFRelease(addressBook);  

// Creating view controller for a new contact  
ABNewPersonViewController *c = [[ABNewPersonViewController alloc] init];  
[c setNewPersonViewDelegate:self];  
[c setDisplayedPerson:person];  
CFRelease(person);  
[self.navigationController pushViewController:c animated:YES];  
[c release];  
}  
查看更多
登录 后发表回答