2011-03-02 214 views
0

我想从iPhone的电话簿中获取所有电话号码。但是当我尝试使用kABPersonPhoneProperty获取数字时,我的应用崩溃了。代码片段在这里使用kABPersinPhoneProperty时应用程序崩溃

ABAddressBookRef addressBook = ABAddressBookCreate(); 
NSMutableArray *allPeople = (NSMutableArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 
int nPeople = ABAddressBookGetPersonCount(addressBook); 
CFRelease(addressBook); 

for(int i=0; i < nPeople; i++){ 

    ABRecordRef person = [allPeople objectAtIndex:i]; 
    NSString *name = @""; 
    CFTypeRef fName = ABRecordCopyValue(person, kABPersonFirstNameProperty); 
    CFTypeRef lName = ABRecordCopyValue(person, kABPersonLastNameProperty); 
    ABMultiValueRef multi = ABRecordCopyValue(person , kABPersonPhoneProperty); 
    NSString *number = (NSString *)ABMultiValueCopyValueAtIndex(multi, 0); 

    if ([number length] > 0) { 
     if(fName != NULL) 
     { 
      if (lName != NULL) { 
       name = [[NSString stringWithFormat:@"%@", fName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
      } 
      else { 
       name = [[NSString stringWithFormat:@"%@", fName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
      } 
      NSLog(@"Number==>%@ ",number); 
     } 
    } 
    if (fName != NULL) { 
     CFRelease(fName); 
    } 
    if (lName != NULL) { 
     CFRelease(lName); 
    } 
    if (multi != NULL) { 
     CFRelease(multi); 
    } 
} 

[allPeople release]; 

我无法弄清楚这个错误。即使如此,它也不能顺利运行。

运行构建和分析的代码部分

ABMultiValueRef multi = ABRecordCopyValue(person , kABPersonPhoneProperty); 
    NSLog(@"Number===>%d" , ABMultiValueGetCount(multi)); 

请帮我来的时候,我甚至让潜在的内存泄漏。

任何形式的帮助将不胜感激。

在此先感谢

+0

请问你可以把崩溃日志。它会给在这里的人提供非常清楚的想法,这将帮助您真正快速地获得帮助。 – 2011-03-02 11:50:27

+0

最糟糕的部分是应用程序崩溃,没有任何警告或消息 – devsri 2011-03-02 11:56:49

+0

@devsri:崩溃而不通知用户是Apple的方式。 – MusiGenesis 2011-03-02 12:05:00

回答

0

感谢您的答复。我已经找到了解决这个问题的解决方案。我没有发布数字变量,认为引用是作为引用传递的,但它被复制到返回的位置,因此需要在使用后释放数字。

再次感谢所有回复!