我试图获取保存在联系人中的所有电子邮件,我可以在ipod中获取电子邮件,但是当我在iphone上测试它显示数组是null。但我的iphone中有联系人
为什么会这样?
-(IBAction)contactfriends:(id)sender
{
ABAddressBookRef _addressBookRef = ABAddressBookCreate();
NSArray* allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef);
NSMutableDictionary *contactsInformation = [[NSMutableDictionary alloc] initWithCapacity:[allPeople count]];
NSMutableArray *propertyList = [[NSMutableArray alloc] init];
NSLog(@"property lsit %@",propertyList); // I am getting this null
NSLog(@"all people %@",allPeople); // I am getting this null
for (id record in allPeople)
{
CFTypeRef emailProp = ABRecordCopyValue((__bridge ABRecordRef)record, kABPersonEmailProperty);
NSString *email = [((__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProp)) objectAtIndex:0 ];
if (!email) {
email = @"";
}
[propertyList addObject:email];
}
NSLog(@"property lsit %@",propertyList); // I am getting this null
}
答案 0 :(得分:3)
试试这个:
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
for(id person in people){
ABMultiValueRef multiemail = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex j=0; j < ABMultiValueGetCount(multiemail); j++) {
NSString* email = (NSString*)ABMultiValueCopyValueAtIndex(multiemail, j);
[arrAllEmail addObject:email];
NSLog(@"%@",email);
[email release];
}
}
答案 1 :(得分:2)
如果您使用的是iOS6,则需要以编程方式检查访问帐户的权限。
您可以像这样检查代码中的权限
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
// First time access has been granted, add the contact
// add your contacts or get emails
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
// add your contacts or get emails
}
else {
// The user has previously denied access
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Error" message:@"permission denied " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}