2012-02-14 63 views
0

獲取電子郵件地址和姓名我都試過,但它崩潰:如何從iPhone地址

- (NSDictionary *)contacts { 

    NSMutableArray *result = [NSMutableArray array]; 
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); 

    CFRelease(addressBook); 

    NSArray *peopleArray = (NSArray *)people; 

    // Return if there are no contacts in the address book 
    if (peopleArray && peopleArray.count > 0) { 

     for (int i = 0; i <= peopleArray.count - 1; i++) { 

      ABRecordRef person = [peopleArray objectAtIndex:i]; 
      ABRecordID sourceID = ABRecordGetRecordID(person); 

      ABMutableMultiValueRef multiEmail = ABRecordCopyValue(person, kABPersonEmailProperty); 

      NSString *emailAddress = (NSString *) ABMultiValueCopyValueAtIndex(multiEmail, 0); //EXE BAD ACCESS 

      [emailAddress release]; 
      CFRelease(multiEmail); 

      NSLog(@"email address %@", emailAddress); 

      NSString *sourceId = [NSString stringWithFormat:@"%i", sourceID]; 

      NSLog(@"%@", sourceId); 
     } 
    } 

    if (peopleArray) CFRelease(people); 
    return [NSArray arrayWithArray:result]; 
} 
+1

它在哪裏崩潰?什麼是錯誤信息? – DarkDust 2012-02-14 11:12:16

+0

查看更新的答案。 – TheLearner 2012-02-14 11:18:22

+0

請嘗試以下操作:1)構建和分析;修復你得到的任何警告2)啓用殭屍3)看看'multiEmail'是否設置爲合法值,如果不是,則向後工作 – yuji 2012-02-14 11:26:56

回答