2011-12-11 60 views
1

我們的一些應用商店用戶在搜索地址簿時報告了崩潰。 我很迷路,因爲我不能重現這個問題。iPhone - 地址簿搜索崩潰

enter image description here

這有什麼錯我如何查詢地址簿?謝謝!!

+ (NSDictionary *) scanAddressBook 
{ 
    #if TARGET_OS_IPHONE 
    NSUInteger i; 
    CFIndex index; 

    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    NSArray *people = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook); 

    if (people==nil || (people && ([people count] == 0))) 
    { 
     TRACE_LOG(@"scanAddressBook ", @"NO ADDRESS BOOK ENTRIES TO SCAN"); 
     if(people) [people release]; 
     CFRelease(addressBook); 
     return nil; 
    } 

    NSMutableArray *numbersArray = [NSMutableArray new]; 
    NSMutableArray *mailsArray = [NSMutableArray new]; 

    for (i=0; i<[people count]; i++) 
    { 
     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 

     ABRecordRef person = (ABRecordRef)[people objectAtIndex:i]; 

     NSMutableDictionary *phoneDictionary = [NSMutableDictionary new]; 

     CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); 
     CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); 

     NSString* log =[NSString stringWithFormat:@"-----CONTACT ENTRY -> %@ : %@", firstName, lastName ]; 
     TRACE_LOG(@"scanAddressBook",log); 


     NSString *userName = @"NoName"; 
     if (firstName && lastName) 
      userName = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; 
     else if (firstName) 
      userName = [NSString stringWithFormat:@"%@", firstName]; 
     else if (lastName) 
      userName = [NSString stringWithFormat:@"%@", lastName]; 

     if(firstName) CFRelease(firstName); 
     if(lastName) CFRelease(lastName); 
     // 
     // Phone Numbers 
     // 
     ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); 
     CFIndex phoneNumberCount = ABMultiValueGetCount(phoneNumbers); 

     for (index=0; index<phoneNumberCount; index++) 
     { 
      CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, index); 
      CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex(phoneNumbers, index); 
      CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel(phoneNumberLabel);  
      // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile" 
      // Find the ones you want here 
      // 

      NSString* log =[NSString stringWithFormat:@"-----PHONE ENTRY -> %@ : %@", phoneNumberLocalizedLabel, phoneNumberValue ]; 
      TRACE_LOG(@"scanAddressBook",log); 

      if (![NetworkingUtils validatePhoneNumber:(NSString *)phoneNumberValue]) { 
       NSLog(@"invalid phone number: %@",phoneNumberValue); 
       CFRelease(phoneNumberLocalizedLabel); 
       CFRelease(phoneNumberLabel); 
       CFRelease(phoneNumberValue); 
       continue; 
      } 

      [phoneDictionary setObject:(NSString *)phoneNumberValue forKey:InviteUserDataNumberKeyID]; 
      [phoneDictionary setObject:(NSString *)phoneNumberLocalizedLabel forKey:InviteUserDataNumberTypeKeyID]; 
      [phoneDictionary setObject:(NSString *)userName forKey:InviteUserDataNameTypeKeyID]; 

      CFRelease(phoneNumberLocalizedLabel); 
      CFRelease(phoneNumberLabel); 
      CFRelease(phoneNumberValue); 
      NSMutableDictionary *copyPhoneDict = [phoneDictionary copy]; 
      [numbersArray addObject:copyPhoneDict]; 
      [copyPhoneDict release]; 
     } 
     CFRelease(phoneNumbers); 
     [phoneDictionary release]; 

     NSMutableDictionary *mailDictionary = [NSMutableDictionary new]; 
     ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty); 
     CFIndex mailsNumberCount = ABMultiValueGetCount(emails); 
     for (index=0; index < mailsNumberCount; index++) 
     { 
      CFStringRef emailValue = ABMultiValueCopyValueAtIndex(emails,index); 
      // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile" 
      // Find the ones you want here 
      // 
      NSString* log =[NSString stringWithFormat:@"-----EMAIL ENTRY -> : %@", emailValue ]; 
      TRACE_LOG(@"scanAddressBook",log); 



      if (![NetworkingUtils validateEmail:(NSString *)emailValue]) { 
       NSLog(@"invalid email address: %@",(NSString *)emailValue); 
       if (emailValue) { 
        CFRelease(emailValue); 
       } 
       continue; 
      } 

      [mailDictionary setObject:(NSString *)emailValue forKey:InviteUserDataMailKeyID]; 
      [mailDictionary setObject:(NSString *)@"email" forKey:InviteUserDataMailTypeKeyID]; 
      [mailDictionary setObject:(NSString *)userName forKey:InviteUserDataMailOwnerKeyID]; 

      if (emailValue) { 
       CFRelease(emailValue); 
      } 

      NSMutableDictionary *copyMailDict = [mailDictionary copy]; 
      [mailsArray addObject:copyMailDict]; 
      [copyMailDict release]; 
     } 
     if(emails) CFRelease(emails); 
     [mailDictionary release]; 


     [pool drain]; 
    } 

    NSString *countryCode = [[NSUserDefaults standardUserDefaults] objectForKey:RequestUserCountryCodeKeyID]; 
    if (!countryCode) { 
     NSLocale *locale = [NSLocale currentLocale]; 
     NSString *aCode = [locale objectForKey: NSLocaleCountryCode]; 
     countryCode = [[NetworkingUtils codesByCountryCode] objectForKey:aCode]; 
    } 

    NSDictionary *aDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [[numbersArray copy] autorelease], InviteUsersNumbersArrayKeyID, 
           [[mailsArray copy] autorelease], InviteUsersMailsArrayKeyID, 
           countryCode, RequestUserCountryCodeKeyID, nil]; 

    [numbersArray release]; 
    [mailsArray release]; 

    [people release]; 
    CFRelease(addressBook); 

    return aDictionary; 
#else 
    return nil ; 
#endif 
} 

回答

3

CFRelease()如果您提供NULL值,則會崩潰。我看到您的大多數CFRelease()調用確實檢查NULL,但不是全部。

最有可能其中之一是觸發崩潰?

我不是很熟悉ABAddressBook,所以不知道他們會在什麼情況下返回NULL

相關提示,Objective-C的-release方法不會崩潰的nil,所以你不妨改變:if(people) [people release];只是[people release];如在Objective-C會悄悄地什麼也不做的所有方法,如果發送到nil

+0

這是個好主意。將嘗試添加更多驗證。 – Idan