有许多应用程序与其后端服务器进行AddressBook同步,以交叉检查AddressBook中的哪些联系人正在使用他们的应用程序,以及需要将哪些用户邀请到他们的应用程序。
这是第一次可以进行完全同步,但之后它不应该是完全同步。
我的第一个问题是,将完整AddressBook与后端服务器同步的最佳方法是什么?
第二个问题是,如何同步仅最近修改过的联系人?
如果有任何示例应用程序或教程请与我分享。
提前致谢。
答案 0 :(得分:1)
您好我也在研究相同的概念。
首先,我没有任何与您分享的教程。但是我在申请中正在做什么,我正在与你分享整个流程。
当应用程序第一次运行时,我将获取所有联系号码并发送到服务器。
但是当任何联系人修改时我只发送修改过的联系电话号码。
修改联系人后,您将收到来自地址簿的回电。每个联系人都会有特定的记录ID
夫特
typealias ABExternalChangeCallback = CFunctionPointer<((ABAddressBook!,CFDictionary!,UnsafeMutablePointer) - > Void)>
通过此属性,您可以了解最近修改了联系人的时间。
ABRecordCopyValue(record,kABPersonModificationDateProperty).takeRetainedValue()为?的NSDate
因此,您可以检查最近修改过的联系号码,然后再将这些联系人发送到服务器。
我希望它可以帮到你!
谢谢
答案 1 :(得分:0)
从地址簿中获取所有电话号码作为格式化字符串,并将此字符串发布到网络服务器。这样可以很容易地在短时间内发送所有联系人。在网络服务器端,您可以编码将电话号码拆分为单独的号码,并检查具有相同号码的用户。
-(NSString *)getPhoneContactAsSingleString
{
NSString *numberString=[NSString new];
numberString=@"";
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
#ifdef DEBUG
//NSLog(@"Fetching contact");
#endif
NSMutableArray *contacts=[[NSMutableArray alloc]init];
NSMutableDictionary *numberNameDictionary=[[NSMutableDictionary alloc]init];
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = CFArrayGetCount(allPeople);
for (int i = 0; i < nPeople; i++)
{
NSMutableDictionary *dictionary=[[NSMutableDictionary alloc]init];
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
if (person) {
NSString *firstname=[NSString new];
NSString *lastname =[NSString new];
CFStringRef firstnameStringRef=ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef lastnameStringRef=ABRecordCopyValue(person, kABPersonLastNameProperty);
if (firstnameStringRef) {
firstname=[NSString stringWithFormat:@"%@",firstnameStringRef];
}
if (lastnameStringRef) {
lastname =[NSString stringWithFormat:@"%@",lastnameStringRef];
}
NSString *name=[NSString new];
if (firstname.length!=0)
{
name=firstname;
if (lastname.length!=0) {
name=[name stringByAppendingString:@" "];
name=[name stringByAppendingString:lastname];
}
}
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
NSString *braketStripedNum = [phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
braketStripedNum = [braketStripedNum stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber=braketStripedNum;
NSString *dashStripedNum = [self clean_phonenumber:phoneNumber with_string:@"-"];
phoneNumber=dashStripedNum;
NSString *comaStripedNum = [self clean_phonenumber:phoneNumber with_string:@","];
phoneNumber=comaStripedNum;
NSString *dotStripedNum = [self clean_phonenumber:phoneNumber with_string:@"."];
phoneNumber=dotStripedNum;
NSString *spaceStripedNum = [self clean_phonenumber:phoneNumber with_string:@" "];
phoneNumber=spaceStripedNum;
if (phoneNumber.length>10) {
phoneNumber=[phoneNumber substringFromIndex:phoneNumber.length-10];
}
if(phoneNumber.length!=0){
[phoneNumbers addObject:phoneNumber];
[numberNameDictionary setObject:name forKey:phoneNumber];
//NSLog(@"[%@] --> [%@]",orginal_phonenumber,phoneNumber);
numberString=[NSString stringWithFormat:@"%@%@,",numberString,phoneNumber];
}
}
//NSLog(@"%@",phoneNumbers);
if (phoneNumbers.count!=0) {
[dictionary setObject:name forKey:@"name"];
[dictionary setObject:phoneNumbers forKey:@"phonenumbers"];
[contacts addObject:dictionary];
}
}
}
if (numberString.length!=0) {
numberString=[numberString substringToIndex:[numberString length]-1];
}
}
else
{
#ifdef DEBUG
NSLog(@"Cannot fetch Contacts :( ");
#endif
}
return numberString;
}
清理电话号码
-(NSString *)clean_phonenumber:(NSString *)number with_string:(NSString *)string
{
NSArray *numberStrips = [number componentsSeparatedByString:string];
NSString *cleanNumber = @"";
for (NSString *eachString in numberStrips) {
cleanNumber = [NSString stringWithFormat:@"%@%@", cleanNumber, eachString];
}
return cleanNumber;
}
只需检查方法
NSLog(@"%@",[self getPhoneContactAsSingleString]);