如何解决iPhone:在没有任何用户界面的情况下将联系人插入到通讯簿中[重复]
|| 这个问题已经在这里有了答案:解决方法
完成答案以将联系人添加到地址簿
添加地址簿框架
AddressBook.framework
和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\",kABPersonJobTitleProperty,@\"Tech. director\",kABPersonDepartmentProperty,@\"iPhone development department\",kABPersonOrganizationProperty,@\"Fuerte international\",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\",@\"08701234567\",(CFStringRef)@\"0870\",NULL);
ABRecordSetValue(person,kABPersonPhoneProperty,phoneNumberMultiValue,nil);
CFRelease(phoneNumberMultiValue);
// Adding url
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue,@\"http://www.fuerteint.com\",kABPersonHomePageLabel,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\",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];
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。