iPhone:无需任何用户界面即可将联系人插入地址簿

时间:2011-04-20 06:54:49

标签: iphone ios4 iphone-sdk-3.0 addressbook abaddressbook

我正在处理地址簿和联系人相关功能。我想在没有任何用户交互的情况下将联系人添加到我的设备。使用ABPerson类我们得到一些用户界面,但我关心的是

我在自己的服务器上有联系人,我想将所有内容从服务器复制到我的iPhone。因此,如果我将使用用户界面,那么它将消耗大量时间。

任何人都可以帮助我摆脱这种情况

提前致谢,

1 个答案:

答案 0 :(得分:5)

完整的答案添加联系人到地址簿   添加地址簿框架AddressBook.frameworkAddressBookUI.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];  
}