0

連絡先に保存されているすべてのメールを取得しようとしましたが、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

}

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 に答える