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*)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

在安慰它说

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

你能检查我的代码,你能更新一些东西吗?我试图解决这个问题很长时间,但没有任何帮助

注意:应用程序 10 次中有 2 次没有崩溃

4

2 回答 2

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





}
于 2013-04-22T04:35:50.493 回答
0
d obj = [arr objectAtIndex:1];
if ([obj isKindOfClass:[NSString class])
{
    // It's an NSString, do something with it...
    NSString *str = obj;
    ...
}
于 2013-08-01T05:53:04.823 回答