public
Created — forked from 0xced/README.md

ABGetMe implementation for iOS

  • Download Gist
emailAddresses.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// Application must link with the MessageUI framework
NSArray* AccountEmailAddresses(void)
{
NSMutableArray *addresses = [NSMutableArray array];
@try
{
NSString *MailAccountProxy = [[NSArray arrayWithObjects:@"Mail", @"Account", @"Proxy", nil] componentsJoinedByString:@""];
NSString *MFMailAccountProxy = [@"MF" stringByAppendingString:MailAccountProxy];
Class MailAccountProxyClass = NSClassFromString(MFMailAccountProxy) ?: NSClassFromString(MailAccountProxy);
SEL reloadAccounts = NSSelectorFromString([[NSArray arrayWithObjects:@"reload", @"Accounts", nil] componentsJoinedByString:@""]);
SEL mailAccounts = NSSelectorFromString([[NSArray arrayWithObjects:@"mail", @"Accounts", nil] componentsJoinedByString:@""]);
SEL emailAddresses = NSSelectorFromString([[NSArray arrayWithObjects:@"email", @"Addresses", nil] componentsJoinedByString:@""]);
[MailAccountProxyClass performSelector:reloadAccounts];
for (id mailAccount in [MailAccountProxyClass performSelector:mailAccounts])
{
for (id emailAddress in [mailAccount performSelector:emailAddresses])
{
if ([emailAddress isKindOfClass:[NSString class]])
[addresses addObject:emailAddress];
}
}
}
@catch (NSException *e) {}
return [NSArray arrayWithArray:addresses];
}
 
 
ABRecordRef ABGetMe(ABAddressBookRef addressBook)
{
ABRecordRef me = NULL;
NSArray *accountEmailAddresses = AccountEmailAddresses();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex peopleCount = CFArrayGetCount(people);
for (CFIndex i = 0; i < peopleCount; i++)
{
ABRecordRef record = CFArrayGetValueAtIndex(people, i);
ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);
if (emails)
{
CFIndex emailCount = ABMultiValueGetCount(emails);
for (CFIndex j = 0; j < emailCount; j++)
{
CFStringRef email = ABMultiValueCopyValueAtIndex(emails, j);
if (email)
{
if ([accountEmailAddresses containsObject:(id)email])
me = record;
CFRelease(email);
}
if (me)
break;
}
CFRelease(emails);
}
if (me)
break;
}
CFRelease(people);
return me;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.