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

发布于 2024-11-02 13:41:04 字数 200 浏览 8 评论 0原文

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

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

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

提前致谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大海や 2024-11-09 13:41:04

将联系人添加到地址簿的完整答案
添加地址簿框架 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, @"[email protected]", (CFStringRef)@"Global", NULL);  
ABMultiValueAddValueAndLabel(emailMultiValue, @"[email protected]", (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];  
}  

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, @"[email protected]", (CFStringRef)@"Global", NULL);  
ABMultiValueAddValueAndLabel(emailMultiValue, @"[email protected]", (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];  
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文