Is there any way to get the “Me” card from iPhone Address Book API?

后端 未结 4 834
傲寒
傲寒 2021-02-07 04:23

So I\'m stumped on this one.

In Mac OS X there is an easy way to get the \"Me\" card (the owner of the Mac/account) from the built-in address book API.

Has a

4条回答
  •  忘了有多久
    2021-02-07 04:41

    I came up with a partial solution to this

    you can get the device name as follows

    NSString *ownerName = [[UIDevice currentDevice] name];
    

    in English a device is originally called, for example, 'Joe Blogg's iPhone'

    the break out the name

     NSRange t = [ownerName rangeOfString:@"’s"];
        if (t.location != NSNotFound) {
            ownerName = [ownerName substringToIndex:t.location];
        }
    

    you can then take that name and search the contacts

    CNContactStore *contactStore = [CNContactStore new];

     NSPredicate *usersNamePredicate = [CNContact predicateForContactsMatchingName:usersName];
    
        NSArray * keysToFetch = @[[CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName],CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNContactSocialProfilesKey, ];
    
     NSArray * matchingContacts = [contactStore unifiedContactsMatchingPredicate:usersNamePredicate keysToFetch:keysToFetch error:nil];
    

    Of course other languages differ in the device name string e.g. 'iPhone Von Johann Schmidt' so more parsing needs to be done for other languages and it only works if the user hasn't changed the name of the device in iTunes to something like "Joes phone' but it gives you a starting point

    well... it gives you an array of matching items :) So if there is more than one contact with that array you just have to use pot luck and go with the first one or work thru multiple cards and take what you need from each.

    I did say its a partial solution and even though it won't work for all user cases you might find it works for many of your users and reduces a little friction

提交回复
热议问题