1
從通訊簿獲取聯繫人詳細信息時,我遇到內存泄漏問題。以下是我提取細節的代碼。誰能幫我?地址簿中的內存泄漏問題iOS
NSMutableDictionary *mycontacts = [[NSMutableDictionary alloc] init];
ABAddressBookRef addressBookRef =ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() ==kABAuthorizationStatusNotDetermined)
{
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
{
if (granted)
{
ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, NULL);
//###MEMORY LEAK LINE
NSArray * allPeople = (__bridge NSArray*) ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople =ABAddressBookGetPersonCount(addressBook);
for(NSUInteger i = 0; i < numberOfPeople; i++)
{
ABRecordRef person =CFArrayGetValueAtIndex((__bridge CFArrayRef)(allPeople), i);
NSString *mobileNumber =[[NSString alloc]init];
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person,kABPersonFirstNameProperty));
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,kABPersonPhoneProperty);
for (CFIndex i = 0; i <ABMultiValueGetCount(phoneNumbers); i++)
{
NSString *phoneNumber = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumbers, i);
if(phoneNumber.length>0)
{
mobileNumber=phoneNumber;
mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@"." withString:@""];
mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
NSString *unfilteredString = mobileNumber;
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890"] invertedSet];
NSString*updatedmobileNumber = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars]componentsJoinedByString:@""];
[mycontacts setObject:updatedmobileNumber forKey:firstName];
break;
}
if(phoneNumber !=nil)
{
CFRelease((__bridge CFTypeRef)(phoneNumber));
}
}
if(phoneNumbers !=nil)
{
CFRelease(phoneNumbers);
}
if(person !=nil)
{
CFRelease(person);
}
if(firstName !=nil)
{
CFRelease((__bridge CFTypeRef)(firstName));
}
}
if(allPeople !=nil)
{
CFRelease((__bridge CFTypeRef)(allPeople));
}
}
else
{
// User denied access
// Display an alert telling user the contact could not be added
}
});
}