我們正在使用CNContactStore從iphone獲取聯繫人,但是我們正在從我的iPhone獲取所有聯繫人,這些聯繫人也提供所有同步的聯繫人,如Facebook和其他Gmail聯繫人,但我只需要獲取存儲在iphone中的聯繫人。如何做到這一點,請幫助我擺脫這一點。聯繫徵詢問題
Q
聯繫徵詢問題
0
A
回答
0
進口這些
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
- (void) fetchContactsFromDevice {
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil) {
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:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
});
}
CFRelease(addressBook);
});
} else {
NSLog(@"Error reading Address Book : %@", error);
}
}
這種方法將幫助你在只讀取通訊錄號碼
,然後用它來繪製
- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook {
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger numberOfPeople = [allPeople count];
for (NSInteger i = 0; i < numberOfPeople; i++) {
ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
NSMutableDictionary * user = [[NSMutableDictionary alloc] init];
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
[user setObject:firstName forKey:@"firstname"];
[user setObject:lastName forKey:@"lastname"];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
[user setObject:phoneNumber forKey:@"phoneNumbers"];
}
CFRelease(phoneNumbers);
[self.contactDataArray addObject:user];
}
[yourtable reloadData];
}
希望這會有所幫助
相關問題
- 1. 聯繫表問題
- 2. 聯繫人創建問題
- 3. 聯繫表格PHP問題
- 4. ASP.NET聯繫表單問題
- 5. 聯繫人問題Contracts.Intents.SHOW_OR_CREATE_CONTACT
- 6. AJAX聯繫表單問題
- 7. iOS聯繫表格問題
- 8. Android聯繫表問題
- 9. PhoneGap的聯繫API問題
- 10. rails聯繫表單問題
- 11. 聯繫表格7問題
- 12. Php聯繫表格問題
- 13. Box2D的聯繫(isTouching)問題
- 14. HTML聯繫表單問題?
- 15. PHP聯繫表格問題
- 16. Android聯繫人問題
- 17. Google聯繫人API問題
- 18. 問題MySQL聯合查詢
- 19. 關聯查詢問題
- 20. 查詢問題聯盟查詢MS Access
- 21. 檢索聯繫人時出現重複聯繫人問題
- 22. 關係MySQL查詢問題
- 23. MYSQL關係查詢問題
- 24. 關係查詢問題:/
- 25. 爲聯繫應用程序提前mysql查詢問題
- 26. 配置問題與PHP聯繫表
- 27. 谷歌聯繫人API的OAuth2問題
- 28. 問題在獲取聯繫信息
- 29. 聯繫人7表單樣式問題
- 30. Yahoo聯繫API Json deserilization問題
任何人都有關於上述問題的知識 – Vetri
如果你看'[store containersMatchingPredicate:nil error:&error]',你會得到多個命中(即,一個用於FB,一個用於Gmail等)?如果是這樣,你可以使用'predicateForContactsInContainerWithIdentifier:'來獲取感興趣的容器的聯繫人。 – Rob