Platforms to show: All Mac Windows Linux Cross-Platform

/Mac64bit/Contacts/Contacts Display Person
Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin, MBS MacCloud Plugin, MBS Mac64bit Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Mac64bit/Contacts/Contacts Display Person
This example is the version from Fri, 13th May 2021.
Project "Contacts Display Person.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 List Inherits Listbox
ControlInstance List Inherits Listbox
EventHandler Sub ExpandRow(row As Integer) dim c as CNContactMBS = me.RowTag(row) if c = nil then break return end if // you could use properties directly dim familyName as string = C.familyName // but we simply try generically all keys showValue c, c.CNContactNamePrefixKey showValue c, c.CNContactGivenNameKey showValue c, c.CNContactMiddleNameKey showValue c, c.CNContactFamilyNameKey showValue c, c.CNContactPreviousFamilyNameKey showValue c, c.CNContactNameSuffixKey showValue c, c.CNContactNicknameKey showValue c, c.CNContactPhoneticGivenNameKey showValue c, c.CNContactPhoneticMiddleNameKey showValue c, c.CNContactPhoneticFamilyNameKey showValue c, c.CNContactOrganizationNameKey showValue c, c.CNContactDepartmentNameKey showValue c, c.CNContactJobTitleKey showValue c, c.CNContactBirthdayKey showValue c, c.CNContactNonGregorianBirthdayKey showValue c, c.CNContactNoteKey showValue c, c.CNContactImageDataKey showValue c, c.CNContactThumbnailImageDataKey showValue c, c.CNContactImageDataAvailableKey showValue c, c.CNContactPhoneNumbersKey showValue c, c.CNContactEmailAddressesKey showValue c, c.CNContactPostalAddressesKey showValue c, c.CNContactDatesKey showValue c, c.CNContactUrlAddressesKey showValue c, c.CNContactRelationsKey showValue c, c.CNContactSocialProfilesKey showValue c, c.CNContactInstantMessageAddressesKey showValue c, c.CNContactIdentifierKey showValue c, c.CNContactTypeKey End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control namefield Inherits TextField
ControlInstance namefield Inherits TextField
End Control
Control SearchButton Inherits PushButton
ControlInstance SearchButton Inherits PushButton
EventHandler Sub Action() List.DeleteAllRows dim contacts() as CNContactMBS dim predicate as NSPredicateMBS dim keysToFetch() as CNKeyDescriptorMBS dim error as NSErrorMBS // we simply ask for all keys which the vcard would also use keysToFetch.append CNContactVCardSerializationMBS.descriptorForRequiredKeys 'keysToFetch.Append CNContactMBS.CNContactOrganizationNameKey dim parts() as string = split(namefield.Text, " ") for each part as string in parts part = trim(part) if part.len > 0 then Dim p As NSPredicateMBS = CNContactMBS.predicateForContactsMatchingName(part) If p = Nil Then // failed? Elseif predicate = Nil Then // first one predicate = p else // combine with AND predicate = NSCompoundPredicateMBS.andPredicateWithSubpredicates(array(predicate, p)) end if end if next if true then // 1. unifiedContactsMatchingPredicate contacts = m.unifiedContactsMatchingPredicate(predicate, keysToFetch, error) else // 2. ContactsWithFetchRequest dim fr as new CNContactFetchRequestMBS(keysToFetch) fr.predicate = predicate fr.mutableObjects = false fr.unifyResults = true contacts = m.ContactsWithFetchRequest(fr, error) end if List.AddRow str(contacts.Ubound+1)+" contacts found" if error <> nil then List.AddRow "Error: "+error.LocalizedDescription end if dim formatter as new CNContactFormatterMBS for each contact as CNContactMBS in contacts List.AddFolder formatter.stringFromContact(contact) List.RowTag(List.LastIndex) = contact next // exapand first one if list.listcount > 1 then List.Expanded(1) = true end if End EventHandler
End Control
EventHandler Sub Open() m = new MyContactStore ShowAuthorization m.requestAccessForEntityType m.CNEntityTypeContacts End EventHandler
Function Address(p as CNPostalAddressMBS) As string // create a list of text values with comma inbetween Dim texts() As String Dim Street As String = p.Street If Street.Len > 0 Then texts.Append street end if Dim PostalCode As String = p.PostalCode Dim City As String = p.City Dim PostalCodeCity() As String If PostalCode.Len > 0 Then // have a zip? PostalCodeCity.Append PostalCode End If If City.Len > 0 Then PostalCodeCity.Append City End If // join zip and city if we have at least one of them If PostalCodeCity.Ubound >= 0 Then texts.Append Join(PostalCodeCity, " ") End If Dim Country As String = p.Country If Country.Len > 0 Then texts.Append Country End If Return Join(texts, ", ") End Function
Sub Log(s as string) list.AddRow s list.ScrollPosition = list.ListCount End Sub
Sub ShowAuthorization() dim x as integer = m.authorizationStatusForEntityType(m.CNEntityTypeContacts) Select case x case m.CNAuthorizationStatusAuthorized log "Authorization status: Authorized" SearchButton.Enabled = true case m.CNAuthorizationStatusDenied log "Authorization status: Denied" case m.CNAuthorizationStatusNotDetermined log "Authorization status: Not Determined" case m.CNAuthorizationStatusRestricted log "Authorization status: Restricted" SearchButton.Enabled = true else log "Authorization status: "+str(x) break end Select End Sub
Sub showValue(c as CNContactMBS, Key as string) If key.Len = 0 Then Return if c.isKeyAvailable(key) then dim v as Variant = c.valueForKey(key) dim l as string = c.localizedStringForKey(key) if l = "" then l = key end if dim type as integer = v.Type Select case Type case Variant.TypeNil // nothing to report #if RBVersion < 2013 then case Variant.TypeInteger, Variant.TypeLong #else case Variant.TypeInt32, Variant.TypeInt64 #endif // just some number List.AddRow l + ": " + v.StringValue case Variant.TypeString // just some text dim t as string = v.StringValue if t <> "" then List.AddRow l + ": " + t end if case Variant.TypeObject If v IsA NSDateComponentsMBS Then Dim n As NSDateComponentsMBS = v List.AddRow l + ": " + n.description Elseif v IsA MemoryBlock then // ignore image data Else // something not handled? Break End If case Variant.TypeObject + Variant.TypeArray dim items() as string dim values() as Object = v if values.Ubound < 0 then Return // empty for each obj as Variant in values If obj IsA CNPhoneNumberMBS Then Dim p As CNPhoneNumberMBS = obj items.Append p.stringValue Elseif obj IsA CNPostalAddressMBS Then Dim p As CNPostalAddressMBS = obj items.Append address(p) Elseif obj IsA CNLabeledValueMBS Then Dim lv As CNLabeledValueMBS = obj Dim va As Variant = lv.Value Dim sa As String If va IsA CNPostalAddressMBS Then Dim po As CNPostalAddressMBS = va sa = address(po) Elseif va IsA CNPhoneNumberMBS Then Dim pn As CNPhoneNumberMBS = va sa = pn.stringValue Elseif va IsA CNInstantMessageAddressMBS Then Dim im As CNInstantMessageAddressMBS = va sa = im.service+": "+im.username Elseif va IsA CNSocialProfileMBS Then Dim sp As CNSocialProfileMBS = va sa = sp.service+": "+sp.username Elseif va IsA CNContactRelationMBS Then Dim cr As CNContactRelationMBS = va sa = cr.Name Elseif va IsA NSDateComponentsMBS Then Dim n As NSDateComponentsMBS = va sa = n.description Else sa = va.StringValue end if items.Append lv.localizedStringForLabel(lv.Label)+": "+sa else // something not handled? Break end if next List.AddRow l + ": " + Join(items, ", ") else // something not handled? Break end Select end if 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" End EventHandler
EventHandler Sub requestAccessForEntityType(granted as boolean, error as NSErrorMBS, tag as Variant) MainWindow.log "requestAccessForEntityType completed" if granted then MainWindow.log "permissions granted" else MainWindow.log "Permissions denied" end if MainWindow.ShowAuthorization 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.

Feedback: Report problem or ask question.

The biggest plugin in space...

Start Chat