William23668 8 Posted December 29, 2022 Hi I was trying to add phone number in the address book demo like that hard coded but the number was never saved, why is that ? Contact.Phones.AddPhone('Home', '767867679678'); procedure TForm1.ActionAddContactExecute(Sender: TObject); var Contact: TAddressBookContact; eMails: TContactEmails; Photo: TBitmapSurface; begin Contact := AddressBook1.CreateContact(FDefaultSource); try try Contact.FirstName := edtFirstName.Text; Contact.LastName := edtLastName.Text; Contact.Phones.AddPhone('Home', '767867679678'); //<<<-------- if not Image1.Bitmap.Size.IsZero then begin Photo := TBitmapSurface.Create; try Photo.Assign(Image1.Bitmap); Contact.Photo := Photo; Image1.Bitmap.SetSize(0, 0); finally Photo.Free; end; end; // Add the work mail eMails := TContactEmails.Create; try eMails.AddEmail(TContactEmail.TLabelKind.Work, edtWorkMail.Text); Contact.eMails := eMails; finally eMails.Free; end; // Save a newly created contact to Address Book AddressBook1.SaveContact(Contact); except on E: EAddressBookException do ShowMessage('Cannot create the contact.' + E.Message); end; // Add the contact to the selected group, if any try if InRange(ComboBox1.ItemIndex, 0, FGroups.Count - 1) then AddressBook1.AddContactIntoGroup (FGroups.Items[ComboBox1.ItemIndex], Contact); except on E: EAddressBookException do ShowMessage('Cannot add the newly created contact to group.' + E.Message); end; ListViewContacts.BeginUpdate; try AddListViewItem(Contact); finally ListViewContacts.EndUpdate; end; TabControl1.ActiveTab := TabItemContacts; finally Contact.Free; end; // Clear the Add Contact Form ClearAddContactForm; end; Share this post Link to post
programmerdelphi2k 237 Posted December 29, 2022 (edited) do you try some like this: var Contact: TAddressBookContact; Phones: TContactPhones; begin Contact := AddressBook1.CreateContact(AddressBook1.DefaultSource); try // Add a mobile phone number Phones := TContactPhones.Create; try Phones.AddPhone(TContactPhone.TLabelKind.Mobile, '+33-6-46-51-3531'); Contact.Phones := Phones; finally Phones.Free; end; // Save the newly created contact AddressBook1.SaveContact(Contact); finally Contact.Free; end; end; Edited December 29, 2022 by programmerdelphi2k 1 Share this post Link to post
William23668 8 Posted December 29, 2022 @programmerdelphi2k I tried like you said but still number was not added. Also sometimes I get stackoverflow erro and external exception 78 error procedure TForm1.ActionAddContactExecute(Sender: TObject); var Contact: TAddressBookContact; Phones: TContactPhones; eMails: TContactEmails; Photo: TBitmapSurface; begin Contact := AddressBook1.CreateContact(FDefaultSource); try try Contact.FirstName := edtFirstName.Text; Contact.LastName := edtLastName.Text; Phones := TContactPhones.Create; try Phones.AddPhone(TContactPhone.TLabelKind.Mobile, '767867679678'); Contact.Phones := Phones; finally Phones.Free; end; if not Image1.Bitmap.Size.IsZero then begin Photo := TBitmapSurface.Create; try Photo.Assign(Image1.Bitmap); Contact.Photo := Photo; Image1.Bitmap.SetSize(0, 0); finally Photo.Free; end; end; // Add the work mail eMails := TContactEmails.Create; try eMails.AddEmail(TContactEmail.TLabelKind.Work, edtWorkMail.Text); Contact.eMails := eMails; finally eMails.Free; end; // Save a newly created contact to Address Book AddressBook1.SaveContact(Contact); except on E: EAddressBookException do ShowMessage('Cannot create the contact.' + E.Message); end; // Add the contact to the selected group, if any try if InRange(ComboBox1.ItemIndex, 0, FGroups.Count - 1) then AddressBook1.AddContactIntoGroup (FGroups.Items[ComboBox1.ItemIndex], Contact); except on E: EAddressBookException do ShowMessage('Cannot add the newly created contact to group.' + E.Message); end; ListViewContacts.BeginUpdate; try AddListViewItem(Contact); finally ListViewContacts.EndUpdate; end; TabControl1.ActiveTab := TabItemContacts; finally Contact.Free; end; // Clear the Add Contact Form ClearAddContactForm; end; Share this post Link to post
programmerdelphi2k 237 Posted December 29, 2022 (edited) All permissions... was checked on Project-Options? https://docwiki.embarcadero.com/Libraries/Sydney/en/FMX.AddressBook.TCustomAddressBook.RequestPermission https://delphiaball.co.uk/2016/04/22/accessing-address-book-ios-android-taddressbook/ Edited December 29, 2022 by programmerdelphi2k Share this post Link to post
William23668 8 Posted December 29, 2022 I think all already selected. The first name and last name is added after I recieve external exception 68 error !! but no phone number Share this post Link to post
William23668 8 Posted December 30, 2022 found the solution by comment this line Phones.Free; Share this post Link to post
programmerdelphi2k 237 Posted December 30, 2022 (edited) It's right! because if you "Free it" your reference on "phones list" would be "null"... provocating a "AV"! Edited December 30, 2022 by programmerdelphi2k 1 Share this post Link to post
William23668 8 Posted December 30, 2022 (edited) 19 hours ago, programmerdelphi2k said: It's right! because if you "Free it" your reference on "phones list" would be "null"... provocating a "AV"! Understand now thanks Edited December 31, 2022 by William23668 Share this post Link to post