Platforms to show: All Mac Windows Linux Cross-Platform
/Mac64bit/Contacts/Contacts Test
Required plugins for this example: MBS Mac64bit Plugin, MBS MacBase Plugin, MBS Main Plugin, MBS MacCloud Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Mac64bit/Contacts/Contacts Test
This example is the version from Tue, 6th Feb 2023.
Project "Contacts Test.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
#if Target64Bit and TargetMacOS then
if CNContactStoreMBS.available then
// ok
else
MsgBox "please run on OS X 10.11 or newer."
quit
end if
#else
MsgBox "Please run with Xojo as 64-bit app on Mac."
quit
#endif
End EventHandler
End Class
Class MainWindow Inherits Window
Control Listbox1 Inherits Listbox
ControlInstance Listbox1 Inherits Listbox
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
dim ContactStore as CNContactStoreMBS = m
dim logo as Picture = LogoMBS
dim LogoData as MemoryBlock = logo.GetData(logo.FormatPNG)
dim d as new NSDateComponentsMBS
d.year = 2010
d.month = 3
d.day = 15
dim c as new CNMutableContactMBS
c.givenName = "Bob"
c.familyName = "Miller"
c.birthday = d
c.contactType = c.CNContactTypeOrganization
c.jobTitle = "CEO"
c.nickname = "the Boss"
c.organizationName = "Great Software LLC"
c.departmentName = "Executive"
c.imageData = LogoData
// To fetch the note property in iOS 13 or later or macOS 16 or later, add the com.apple.developer.contacts.notes entitlement to your app. The entitlement requires permission from Apple to use, and you can’t publicly distribute your app until you have permission to use it. For more information about adding the entitlement and getting permission, see com.apple.developer.contacts.notes.
'c.note = "loves strawberry cake"
// email
dim emails() as CNLabeledValueMBS
dim email as new CNLabeledValueMBS(CNLabeledValueMBS.CNLabelWork, "bob@greatsoftware.com")
emails.Append email
c.setEmailAddresses emails
// URL
dim URLs() as CNLabeledValueMBS
dim URL as new CNLabeledValueMBS(CNLabeledValueMBS.CNLabelURLAddressHomePage, "http://www.greatsoftware.com")
URLs.Append URL
c.setURLAddresses URLs
// Social profiles
dim SocialProfiles() as CNLabeledValueMBS
dim profile as new CNSocialProfileMBS("http://www.facebook.com", "test", "", CNSocialProfileMBS.CNSocialProfileServiceFacebook)
dim SocialProfile as new CNLabeledValueMBS(CNLabeledValueMBS.CNLabelHome, Profile)
SocialProfiles.Append SocialProfile
c.setSocialProfiles SocialProfiles
// Instant Message Addresses
dim InstantMessageAddresses() as CNLabeledValueMBS
dim InstantMessageAddr as new CNInstantMessageAddressMBS("test", CNInstantMessageAddressMBS.CNInstantMessageServiceFacebook)
dim InstantMessageAddresse as new CNLabeledValueMBS(CNLabeledValueMBS.CNLabelHome, InstantMessageAddr)
InstantMessageAddresses.Append InstantMessageAddresse
c.setInstantMessageAddresses InstantMessageAddresses
// Postal Addresses
dim PostalAddresses() as CNLabeledValueMBS
dim address as new CNMutablePostalAddressMBS
address.City = "TestCity"
address.PostalCode = "12345"
address.Street = "Test Avenue 123"
address.State = "NY"
address.ISOCountryCode = "US"
address.Country = "United States of America"
dim PostalAddress as new CNLabeledValueMBS(CNLabeledValueMBS.CNLabelHome, address)
PostalAddresses.Append PostalAddress
c.setPostalAddresses PostalAddresses
// phone
dim phonenumbers() as CNLabeledValueMBS
dim phonen as new CNPhoneNumberMBS("123456789")
dim phone as new CNLabeledValueMBS(CNLabeledValueMBS.CNLabelWork, phonen)
phonenumbers.Append phone
c.setPhoneNumbers phonenumbers
// save
dim sr as new CNSaveRequestMBS
dim ContainerIdentifier as string = ContactStore.defaultContainerIdentifier
sr.addContact c, ContainerIdentifier
dim e as NSErrorMBS
if ContactStore.executeSaveRequest(sr, e) then
MsgBox "Saved"
else
MsgBox "Failed to save contact"+EndOfLine+EndOfLine+e.localizedDescription
end if
End EventHandler
End Control
EventHandler Sub Open()
m = new MyContactStore
ShowAuthorization
m.requestAccessForEntityType m.CNEntityTypeContacts
ShowContacts
End EventHandler
Sub Log(s as string)
listbox1.AddRow s
listbox1.ScrollPosition = listbox1.ListCount
End Sub
Sub ShowAuthorization()
dim x as integer = m.authorizationStatusForEntityType(m.CNEntityTypeContacts)
Select case x
case m.CNAuthorizationStatusAuthorized
log "Authorization status: Authorized"
case m.CNAuthorizationStatusDenied
log "Authorization status: Denied"
case m.CNAuthorizationStatusNotDetermined
log "Authorization status: Not Determined"
case m.CNAuthorizationStatusRestricted
log "Authorization status: Restricted"
else
log "Authorization status: "+str(x)
break
end Select
End Sub
Sub ShowContacts()
dim keys() as CNKeyDescriptorMBS
dim f as new CNContactFetchRequestMBS(keys)
dim e as NSErrorMBS
dim contacts() as CNContactMBS = m.ContactsWithFetchRequest(f, e)
if e <> nil then
log "Error fetching contacts: "+e.LocalizedDescription
end if
log str(UBound(contacts)+1)+" contacts found."
End Sub
Property m As MyContactStore
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyContactStore Inherits CNContactStoreMBS
EventHandler Sub DidChange()
mainWindow.log "Contacts Changed"
mainwindow.ShowContacts
End EventHandler
EventHandler Sub requestAccessForEntityType(granted as boolean, error as NSErrorMBS, tag as Variant)
#Pragma unused tag
MainWindow.log "requestAccessForEntityType completed"
if granted then
MainWindow.Log "permissions granted"
else
MainWindow.log "Permissions denied"
End If
If error <> Nil Then
MainWindow.Log error.LocalizedDescription
end if
MainWindow.ShowAuthorization
MainWindow.ShowContacts
End EventHandler
End Class
ExternalFile info
End ExternalFile
End Project
See also:
The items on this page are in the following plugins: MBS Mac64bit Plugin.