我正在開發一個應用程序,我想從iphone中的聯繫人列表中獲取正在使用相同應用程序的聯繫人。只提取使用相同的應用程序的手機聯繫人
怎麼辦?任何示例代碼或鏈接?
請幫幫我。
注意:我不想在我的ios應用程序中獲取所有聯繫人,我只想獲取正在使用相同應用程序的聯繫人。
我正在開發一個應用程序,我想從iphone中的聯繫人列表中獲取正在使用相同應用程序的聯繫人。只提取使用相同的應用程序的手機聯繫人
怎麼辦?任何示例代碼或鏈接?
請幫幫我。
注意:我不想在我的ios應用程序中獲取所有聯繫人,我只想獲取正在使用相同應用程序的聯繫人。
它不可能沒有後臺列表中的聯繫人,你不能只在ios應用程序中執行。
導入通訊錄框架第一
然後調用這兩個函數
-(void)AddressBookFetch{
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) {
// if you got here, user had previously denied/revoked permission for your
// app to access the contacts, and all you can do is handle this gracefully,
// perhaps telling the user that they have to go to settings to grant access
// to contacts
[[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
return;
}
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (!addressBook) {
NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
return;
}
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
}
if (granted) {
// if they gave you permission, then just carry on
[self listPeopleInAddressBook:addressBook];
} else {
// however, if they didn't give you permission, handle it gracefully, for example...
dispatch_async(dispatch_get_main_queue(), ^{
// BTW, this is not on the main thread, so dispatch UI updates back to the main queue
[[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
});
}
CFRelease(addressBook);
});
}
然後通過此功能
- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
numberOfPeople = [allPeople count];
for (NSInteger i = 0; i < numberOfPeople; i++) {
ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName);
if (firstName==nil) {
[email protected]"";
}
[nm addObject:firstName];
if (lastName==nil) {
[email protected]"";
}
[ttl addObject:lastName];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
// CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
//for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, 0));
NSLog(@" phone:%@", phoneNumber);
if (phoneNumber==nil) {
[email protected]"";
}
[phn addObject:phoneNumber];
// }
// CFRelease(phoneNumbers);
NSLog(@"=============================================");
}
}
我不想在我的ios應用程序中獲取所有聯繫人,我只想獲取使用相同應用程序的聯繫人。 –
然後,您必須幫助保存所有用戶的數據庫,並且必須檢查哪些用戶與您的聯繫人匹配。 –
[獲取iOS上所有聯繫人的列表](http://stackoverflow.com/questions/3747844/get-a-list-of-all-contacts-on-ios) –
@NimitParekh I don不想檢索所有聯繫人,我只想要檢索使用相同應用程序的聯繫人。 –
當您以編程方式添加聯繫人時,需要添加一些文本,並且在提取聯繫人時,您可以過濾掉這些記錄。 –