0

this is how i add contact information in my database from addressbook

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]; 


}

data get added in sqlite database everything works fine then uialertview appears and everything get stuck when i put breakpoint then it reaches to main.m

 return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

and says exe_bad_access

in consol it says

  Single stepping until exit from function objc_release, 
which has no line number information.
(gdb) 

can you please check my code and can you update something? i am trying to solve this for long time but nothing helped

NOTE: 2 times out of 10 times app is not getting crashed

Prakash Desai
  • 511
  • 3
  • 14

2 Answers2

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]; 
    }





}
Prakash Desai
  • 511
  • 3
  • 14
0
d obj = [arr objectAtIndex:1];
if ([obj isKindOfClass:[NSString class])
{
    // It's an NSString, do something with it...
    NSString *str = obj;
    ...
}
Avi
  • 36
  • 5