0

我试图获取保存在联系人中的所有电子邮件,我可以在 ipod 中获取 eemail,但是当我在 iphone 上测试时,它显示数组为空。但是我的 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

}

4

2 回答 2

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];
            }
    }
于 2013-03-11T11:57:26.123 回答
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];
  }
于 2013-03-11T11:57:04.217 回答