これは、アドレス帳からデータベースに連絡先情報を追加する方法です
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 回はアプリがクラッシュしていません