这就是我在地址簿
中添加数据库中的联系信息的方法ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeopleName = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeopleName = ABAddressBookGetPersonCount(addressBook);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
for( int i = 0 ; i < nPeopleName ; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeopleName, i );
NSString* personName = (__bridge_transfer NSString*) ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSDate* Date = (__bridge_transfer NSDate*) ABRecordCopyValue(ref, kABPersonBirthdayProperty);
CFStringRef value, label;
ABMutableMultiValueRef multi = ABRecordCopyValue(ref, kABPersonEmailProperty);
CFIndex count = ABMultiValueGetCount(multi);
if (count == 1) {
value = ABMultiValueCopyValueAtIndex(multi, 0);
NSString* emailAddress = (NSString*)value;
[emailAddress retain];
CFRelease(value);
[_Emailarray addObject:emailAddress];
NSLog(@"_Emailarray%@",_Emailarray);
} else {
for (CFIndex i = 0; i < count; i++) {
label = ABMultiValueCopyLabelAtIndex(multi, i);
value = ABMultiValueCopyValueAtIndex(multi, i);
// check for home e-mail label
// you can put some if else here like there is already you checking for home but you can check for more like work and that and this and all which can provoide email id to you so as you can see the code which is below you do else if there and get email id from where ever you can got it?
if (label && CFStringCompare(label, kABHomeLabel, 0) == 0) {
NSString* emailAddress = (NSString*)value;
[emailAddress retain];
NSLog(@"date%@",Date);
if (Date != nil) {
[_Emailarray addObject:emailAddress];
}
NSLog(@"_Emailarray%@",_Emailarray);
break;
}
CFRelease(label);
CFRelease(value);
}
}
if (_Emailarray.count == i) {
NSLog(@"_Emailarray.count %d",_Emailarray.count);
[_Emailarray addObject:[NSString stringWithString:@"Add Email"]];
NSLog(@"_Emailarray%@",_Emailarray);
}
CFRelease(multi);
NSString *birthdayDate = [dateFormat stringFromDate:Date];
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSString* mobile=@"";
NSString* mobileLabel;
for (int i=0; i < ABMultiValueGetCount(phones); i++) {
//NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
//NSLog(@"%@", phone);
// right now you are you getting mobile number from some plcaes but check for all the condition which are available in the addressbook and get the phone number just put some more else if condition and there you go got it ?
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
NSLog(@"mobile:");
} else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {
NSLog(@"iphone:");
} else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel]) {
NSLog(@"pager:");
}
[mobile release];
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@", mobile);
NSCharacterSet* charactersToRemove = [[NSCharacterSet decimalDigitCharacterSet] invertedSet] ;
mobile = [[mobile componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""] ;
}
NSString *holderString = [[NSString alloc]initWithString:@"Add Number"];
if (birthdayDate != nil){
[_birthdateincontects addObject:birthdayDate];
NSLog(@"_birthdateincontects%@",_birthdateincontects);
if([mobile isEqualToString:@""]){
[_Phonearray addObject:holderString];
NSLog(@"_Phonearray%@",_Phonearray);
}
else{
[_Phonearray addObject:mobile];
NSLog(@"_Phonearray%@",_Phonearray);
}
if(personName != nil){
[_namesincontects addObject:personName];
NSLog(@"_namesincontects%@",_namesincontects);
}
else{
[_namesincontects addObject:@"No Name"];
NSLog(@"_namesincontects%@",_namesincontects);
}
}
NSLog(@"_namesincontects%@",_namesincontects);
NSLog(@"_birthdateincontects%@",_birthdateincontects);
NSLog(@"_Phonearray%@",_Phonearray);
NSLog(@"_Emailarray%@",_Emailarray);
}
contentString = [[NSMutableString alloc]init];
char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates,Phonenumber,Email,Profilepic) values(?,?,?,?,?);";
sqlite3_stmt *stmt;
int x;
if ((x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil)) != SQLITE_OK)
{
NSLog(@"%s: prepare failed: %s (%d)", __FUNCTION__, sqlite3_errmsg(database1), x);
return;
}
for (int i = 0; i < _birthdateincontects.count; i++)
{
[contentString appendString:[_namesincontects objectAtIndex:i]];
if(i !=_birthdateincontects.count-1){
[contentString appendString:@", "];
}
sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",[_namesincontects objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:@"%@",[_birthdateincontects objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 3, [[NSString stringWithFormat:@"%@",[_Phonearray objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 4, [[NSString stringWithFormat:@"%@",[_Emailarray objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 5,[[NSString stringWithFormat:@"%@",@"No Image"] UTF8String],-1, NULL);
if ((x = sqlite3_step(stmt)) != SQLITE_DONE) {
NSLog(@"%s: step failed: %s (%d)", __FUNCTION__, sqlite3_errmsg(database1), x);
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
NSLog(@"contentString%@",contentString);
if([contentString length] != 0){
[[[UIAlertView alloc] initWithTitle:@"Friends added from AddressBook:"
message:contentString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil]
show];
}
[_namesincontects removeAllObjects];
[_birthdateincontects removeAllObjects];
[_Phonearray removeAllObjects];
[_Emailarray removeAllObjects];
[UIAlertView release];
[contentString release];
}
数据在sqlite数据库中添加一切正常,然后uialertview出现,当我放入断点然后它到达main.m时,一切都卡住了
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
并说exe_bad_access
在consol中说
Single stepping until exit from function objc_release,
which has no line number information.
(gdb)
你可以检查我的代码,你能更新一下吗?我试图长期解决这个问题,但没有任何帮助
注意:10次app中有2次没有崩溃
答案 0 :(得分:0)
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeopleName = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeopleName = ABAddressBookGetPersonCount(addressBook);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
for( int i = 0 ; i < nPeopleName ; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeopleName, i );
NSString* personName = (__bridge_transfer NSString*) ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSDate* Date = (__bridge_transfer NSDate*) ABRecordCopyValue(ref, kABPersonBirthdayProperty);
CFStringRef value, label;
ABMutableMultiValueRef multi = ABRecordCopyValue(ref, kABPersonEmailProperty);
CFIndex count = ABMultiValueGetCount(multi);
if (count == 1) {
value = ABMultiValueCopyValueAtIndex(multi, 0);
NSString* emailAddress = (NSString*)value;
[emailAddress retain];
CFRelease(value);
[_Emailarray addObject:emailAddress];
NSLog(@"_Emailarray%@",_Emailarray);
} else {
for (CFIndex i = 0; i < count; i++) {
label = ABMultiValueCopyLabelAtIndex(multi, i);
value = ABMultiValueCopyValueAtIndex(multi, i);
// check for home e-mail label
// you can put some if else here like there is already you checking for home but you can check for more like work and that and this and all which can provoide email id to you so as you can see the code which is below you do else if there and get email id from where ever you can got it?
if (label && CFStringCompare(label, kABHomeLabel, 0) == 0) {
NSString* emailAddress = (NSString*)value;
[emailAddress retain];
NSLog(@"date%@",Date);
if (Date != nil) {
[_Emailarray addObject:emailAddress];
}
NSLog(@"_Emailarray%@",_Emailarray);
break;
}
//CFRelease(label);
// CFRelease(value);
}
}
if (_Emailarray.count == i) {
NSLog(@"_Emailarray.count %d",_Emailarray.count);
[_Emailarray addObject:[NSString stringWithString:@"Add Email"]];
NSLog(@"_Emailarray%@",_Emailarray);
}
// CFRelease(multi);
NSString *birthdayDate = [dateFormat stringFromDate:Date];
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSString* mobile=@"";
NSString* mobileLabel;
for (int i=0; i < ABMultiValueGetCount(phones); i++) {
//NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
//NSLog(@"%@", phone);
// right now you are you getting mobile number from some plcaes but check for all the condition which are available in the addressbook and get the phone number just put some more else if condition and there you go got it ?
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
NSLog(@"mobile:");
}
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {
NSLog(@"iphone:");
}
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneMainLabel]) {
NSLog(@"main:");
}
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel]) {
NSLog(@"pager:");
}
// [mobile release];
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@", mobile);
NSCharacterSet* charactersToRemove = [[NSCharacterSet decimalDigitCharacterSet] invertedSet] ;
mobile = [[mobile componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""] ;
}
NSString *holderString = [[NSString alloc]initWithString:@"Add Number"];
if (birthdayDate != nil){
[_birthdateincontects addObject:birthdayDate];
NSLog(@"_birthdateincontects%@",_birthdateincontects);
if([mobile isEqualToString:@""]){
[_Phonearray addObject:holderString];
NSLog(@"_Phonearray%@",_Phonearray);
}
else{
[_Phonearray addObject:mobile];
NSLog(@"_Phonearray%@",_Phonearray);
}
if(personName != nil){
[_namesincontects addObject:personName];
NSLog(@"_namesincontects%@",_namesincontects);
}
else{
[_namesincontects addObject:@"No Name"];
NSLog(@"_namesincontects%@",_namesincontects);
}
}
NSLog(@"_namesincontects%@",_namesincontects);
NSLog(@"_birthdateincontects%@",_birthdateincontects);
NSLog(@"_Phonearray%@",_Phonearray);
NSLog(@"_Emailarray%@",_Emailarray);
}
contentString = [[NSMutableString alloc]init];
char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates,Phonenumber,Email,Profilepic) values(?,?,?,?,?);";
sqlite3_stmt *stmt;
int x;
if ((x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil)) != SQLITE_OK)
{
NSLog(@"%s: prepare failed: %s (%d)", __FUNCTION__, sqlite3_errmsg(database1), x);
return;
}
for (int i = 0; i < _birthdateincontects.count; i++)
{
[contentString appendString:[_namesincontects objectAtIndex:i]];
if(i !=_birthdateincontects.count-1){
[contentString appendString:@", "];
}
sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",[_namesincontects objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:@"%@",[_birthdateincontects objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 3, [[NSString stringWithFormat:@"%@",[_Phonearray objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 4, [[NSString stringWithFormat:@"%@",[_Emailarray objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 5,[[NSString stringWithFormat:@"%@",@"No Image"] UTF8String],-1, NULL);
if ((x = sqlite3_step(stmt)) != SQLITE_DONE) {
NSLog(@"%s: step failed: %s (%d)", __FUNCTION__, sqlite3_errmsg(database1), x);
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
NSLog(@"contentString%@",contentString);
if([contentString length] != 0){
UIAlertView *alertForAddressbook=[[UIAlertView alloc] initWithTitle:@"Friends added from AddressBook:" message:contentString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertForAddressbook show];
// [alertForAddressbook release];
//[contentString release];
}
}
答案 1 :(得分:0)
d obj = [arr objectAtIndex:1];
if ([obj isKindOfClass:[NSString class])
{
// It's an NSString, do something with it...
NSString *str = obj;
...
}