Navnath Memane
Navnath Memane

Reputation: 273

Code get crashing at adding phone contacts query

I am able to add only showroom name to my contacts. But when I put the code to add phone numbers, further details....my code get crash. Any suggestion would be appreciated. Here is my code

- (IBAction)AddContact
 {   
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef Showroom = ABPersonCreate();

//adding contact name as showroom name
ABRecordSetValue(Showroom, kABPersonFirstNameProperty, ShowroomName.text , nil);

//adding phone number
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
 ABMultiValueAddValueAndLabel(multiPhone, p_BcardLabel.text, kABPersonPhoneMainLabel, NULL);

 ABRecordSetValue(Showroom, kABPersonPhoneProperty, multiPhone,nil);
 CFRelease(multiPhone);
//adding emailaddress
 ABMutableMultiValueRef multiEmail =  ABMultiValueCreateMutable(kABMultiStringPropertyType);
 ABMultiValueAddValueAndLabel(multiEmail, email_BcardLabel.text, kABWorkLabel,   NULL);
 ABRecordSetValue(Showroom, kABPersonEmailProperty, multiEmail, @"");
 CFRelease(multiEmail);

 //adding URL
 ABMutableMultiValueRef multiURL = ABMultiValueCreateMutable(kABMultiStringPropertyType);
 ABMultiValueAddValueAndLabel(multiURL, url_BcardLabel.text, kABHomeLabel, NULL);
 ABRecordSetValue(Showroom, kABPersonURLProperty, multiURL, @"");
 CFRelease(multiURL);

Shows following message

[Switching to thread 11779]
warning: Unable to read symbols for   /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.2   (8H7)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).

Upvotes: 0

Views: 735

Answers (2)

Anil Kothari
Anil Kothari

Reputation: 7733

This is the Answer to your question Navnath. Apple provides ABNewPersonViewController to open the particular add contact view.Add the ABNewPersonViewControllerDelegate in your header file.

case 1:Application is view based:-

     -(IBAction)displayContactAddWindow:(id)sender{
    ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
    picker.newPersonViewDelegate = self;

    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
    [self presentModalViewController:navigation animated:YES];

    [picker release];
    [navigation release];
}

case 2:Application is navigation based:-

-(IBAction)displayContactAddWindow:(id)sender{

    ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];

    picker.newPersonViewDelegate = self;

    [self.navigationController pushViewController:picker animated:YES];

    [picker release];

   }

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person{

    [self.navigationController popViewControllerAnimated:YES];

}

Upvotes: 0

Anil Kothari
Anil Kothari

Reputation: 7733

If you want only to improve your code. I have checked once your code its fine working just add the following lines

ABAddressBookAddRecord(addressBook,Showroom, nil);


    ABAddressBookSave(addressBook,nil); 

    objABPersonViewController=[[ABUnknownPersonViewController alloc]init];

    objABPersonViewController.displayedPerson=Showroom;

    [self.navigationController pushViewController:objABPersonViewController animated:YES];


    CFRelease(Showroom);

And if you want to use mine check this out. It will also add an image named abc.png from your resource folder to the contact list.

 -(IBAction)addToAddressbook:(id)sender{ 
        NSString *fname=@"Person First Name";

        NSString *lname=@"Person Last Name";

        NSArray *arrayAdd=[[NSArray alloc]initWithObjects:@"street Name",@"city Name",@"country code",@"zip",nil];

        UIImage *image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"abc.png" ofType:nil]];

        [self addContact:fname:lname:arrayAdd:image];
    }

    -(void) addContact:(NSString *)firstname:(NSString *)lastname:(NSArray *)arrayAddress:(UIImage *)currentImage
    {
        ABAddressBookRef addressBook=ABAddressBookCreate();

        ABRecordRef person=ABPersonCreate();

        //set Image
        NSData * dataRef = UIImagePNGRepresentation(currentImage);

        ABPersonSetImageData(person, (CFDataRef)dataRef, nil);

        //set FirstName and LastName
        ABRecordSetValue(person, kABPersonFirstNameProperty,firstname, nil);

        ABRecordSetValue(person, kABPersonLastNameProperty,lastname, nil);


        //Add Address
        ABMutableMultiValueRef address=ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

        NSMutableDictionary *addressDictionary=[[NSMutableDictionary alloc]init];

        [addressDictionary setObject:[arrayAddress objectAtIndex:0] forKey:(NSString *)kABPersonAddressStreetKey];

        [addressDictionary setObject:[arrayAddress objectAtIndex:1]forKey:(NSString *)kABPersonAddressCityKey];

        [addressDictionary setObject:[arrayAddress objectAtIndex:2] forKey:(NSString *)kABPersonAddressCountryCodeKey];

        [addressDictionary setObject:[arrayAddress objectAtIndex:3] forKey:(NSString *)kABPersonAddressCountryKey];

        ABMultiValueAddValueAndLabel(address, addressDictionary, kABHomeLabel, nil);

        ABRecordSetValue(person, kABPersonAddressProperty, address, nil);

        ABAddressBookAddRecord(addressBook,person, nil);

        ABAddressBookSave(addressBook,nil); 

        objABPersonViewController=[[ABUnknownPersonViewController alloc]init];

        objABPersonViewController.displayedPerson=person;

        [self.navigationController pushViewController:objABPersonViewController animated:YES];


        CFRelease(person);

    }

for any further query just ask... Happy to help

Upvotes: 1

Related Questions