Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

A fix for broken address book group handling #5

Closed
wants to merge 3 commits into from

2 participants

@stephenjames

When adding a contact to a group both the group and contact must be created or referenced with the same address book (ABAddressBookCreate()).

Otherwise ABGroup.addMember fails silently with no error.

Revised the code to create one address book and re-use it. This required replacing the class methods with instance methods.

Stephen James Fix broken contact groups
When adding a contact to a group both the group and contact must be created or referenced with the same address book (ABAddressBookCreate()).

Otherwise ABGroup.addMember fails silently with no error.

Revised the code to create one address book and re-use it.  This required replacing the class methods to instance methods.
6f9d796
@erica
Owner
Stephen James added some commits
@erica erica closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 10, 2012
  1. Fix broken contact groups

    Stephen James authored
    When adding a contact to a group both the group and contact must be created or referenced with the same address book (ABAddressBookCreate()).
    
    Otherwise ABGroup.addMember fails silently with no error.
    
    Revised the code to create one address book and re-use it.  This required replacing the class methods to instance methods.
Commits on Aug 16, 2012
  1. Add helper methods for first phone and mobile

    Stephen James authored
Commits on Aug 22, 2012
  1. Redacted updates

    Stephen James authored
This page is out of date. Refresh to see the latest.
View
2  ABContact.h
@@ -113,4 +113,6 @@
+ (id) contactWithDictionary: (NSDictionary *) dict;
+ (id) contactWithData: (NSData *) data;
+- (NSString *) getFirstMainPhone;
+- (NSString *) getFirstMobile;
@end
View
39 ABContact.m
@@ -58,10 +58,13 @@ + (id) contactWithRecord: (ABRecordRef) person
return [[[ABContact alloc] initWithRecord:person] autorelease];
}
-+ (id) contactWithRecordID: (ABRecordID) recordID
-{
- ABAddressBookRef addressBook = ABAddressBookCreate();
++ (id) contactWithRecordID: (ABRecordID) recordID{
+ CFErrorRef error=NULL;
+ ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil, &error);
ABRecordRef contactrec = ABAddressBookGetPersonWithRecordID(addressBook, recordID);
+ if(contactrec == NULL){
+ return nil;
+ }
ABContact *contact = [self contactWithRecord:contactrec];
// CFRelease(contactrec); // Thanks Gary Fung
return contact;
@@ -268,9 +271,8 @@ + (NSDictionary *) smsWithService: (CFStringRef) service andUser: (NSString *) u
}
// Thanks to Eridius for suggestions re: error
-- (BOOL) removeSelfFromAddressBook: (NSError **) error
-{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+- (BOOL) removeSelfFromAddressBook: (NSError **) error{
+ ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreateWithOptions(nil, (CFErrorRef *) error));
if (!ABAddressBookRemoveRecord(addressBook, self.record, (CFErrorRef *) error)) return NO;
return ABAddressBookSave(addressBook, (CFErrorRef *) error);
}
@@ -743,4 +745,29 @@ + (id) contactWithData: (NSData *) data
return [self contactWithDictionary:dict];
}
+- (NSString *) getFirstMainPhone{
+ NSArray *arr0 = self.phoneArray;
+ NSArray *arr1 = self.phoneLabels;
+ NSInteger ix=0;
+ for(NSString *label in arr1){
+ if([label compare:(NSString *)kABPersonPhoneMainLabel]==NSOrderedSame){
+ return [arr0 objectAtIndex:ix];
+ }
+ ix++;
+ }
+ return nil;
+}
+- (NSString *) getFirstMobile{
+ NSArray *arr0 = self.phoneArray;
+ NSArray *arr1 = self.phoneLabels;
+ NSInteger ix=0;
+ for(NSString *label in arr1){
+ if([label compare:(NSString *)kABPersonPhoneMobileLabel]==NSOrderedSame){
+ return [arr0 objectAtIndex:ix];
+ }
+ ix++;
+ }
+ return nil;
+}
+
@end
View
35 ABContactsHelper.h
@@ -11,37 +11,42 @@
#import "ABGroup.h"
@interface ABContactsHelper : NSObject
+@property (nonatomic,assign) ABAddressBookRef addressBook;
++ (ABContactsHelper *)helper;
++ (ABContactsHelper *)helperWithAddressBook:(ABAddressBookRef)book;
+//save the address book
+- (BOOL) save;
// Address Book
-+ (ABAddressBookRef) addressBook;
+//+ (ABAddressBookRef) addressBook;
// Address Book Contacts and Groups
-+ (NSArray *) contacts; // people
-+ (NSArray *) groups; // groups
+- (NSArray *) contacts; // people
+- (NSArray *) groups; // groups
// Counting
-+ (int) contactsCount;
-+ (int) contactsWithImageCount;
-+ (int) contactsWithoutImageCount;
-+ (int) numberOfGroups;
+- (int) contactsCount;
+- (int) contactsWithImageCount;
+- (int) contactsWithoutImageCount;
+- (int) numberOfGroups;
// Sorting
-+ (BOOL) firstNameSorting;
+- (BOOL) firstNameSorting;
// Add contacts and groups
-+ (BOOL) addContact: (ABContact *) aContact withError: (NSError **) error;
-+ (BOOL) addGroup: (ABGroup *) aGroup withError: (NSError **) error;
+- (BOOL) addContact: (ABContact *) aContact withError: (NSError **) error;
+- (BOOL) addGroup: (ABGroup *) aGroup withError: (NSError **) error;
// Find contacts
-+ (NSArray *) contactsMatchingName: (NSString *) fname;
-+ (NSArray *) contactsMatchingName: (NSString *) fname andName: (NSString *) lname;
-+ (NSArray *) contactsMatchingPhone: (NSString *) number;
+- (NSArray *) contactsMatchingName: (NSString *) fname;
+- (NSArray *) contactsMatchingName: (NSString *) fname andName: (NSString *) lname;
+- (NSArray *) contactsMatchingPhone: (NSString *) number;
// Find groups
-+ (NSArray *) groupsMatchingName: (NSString *) fname;
+- (NSArray *) groupsMatchingName: (NSString *) fname;
@end
// For the simple utility of it. Feel free to comment out if desired
@interface NSString (cstring)
@property (readonly) char *UTF8String;
-@end
+@end
View
89 ABContactsHelper.m
@@ -9,17 +9,41 @@
#define CFAutorelease(obj) ({CFTypeRef _obj = (obj); (_obj == NULL) ? NULL : [(id)CFMakeCollectable(_obj) autorelease]; })
@implementation ABContactsHelper
+@synthesize addressBook;
+
+- (void)dealloc{
+ if(self.addressBook)
+ CFRelease(self.addressBook);
+ [super dealloc];
+}
+- (BOOL)save{
+ NSError *error;
+ return ABAddressBookSave(addressBook, (CFErrorRef *) error);
+}
++ (ABContactsHelper *)helper{
+ ABContactsHelper *helper = [[ABContactsHelper alloc] init];
+ CFErrorRef error=NULL;
+ helper.addressBook = ABAddressBookCreateWithOptions(nil, &error);
+ return helper;
+}
+
++ (ABContactsHelper *)helperWithAddressBook:(ABAddressBookRef)book{
+ ABContactsHelper *helper = [[ABContactsHelper alloc] init];
+ helper.addressBook = book;
+ return helper;
+}
+
/*
Note: You cannot CFRelease the addressbook after CFAutorelease(ABAddressBookCreate());
*/
-+ (ABAddressBookRef) addressBook
-{
- return CFAutorelease(ABAddressBookCreate());
-}
+//+ (ABAddressBookRef) addressBook
+//{
+// return CFAutorelease(ABAddressBookCreate());
+//}
-+ (NSArray *) contacts
+- (NSArray *) contacts
{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+ //ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray *array = [NSMutableArray arrayWithCapacity:thePeople.count];
for (id person in thePeople)
@@ -28,15 +52,15 @@ + (NSArray *) contacts
return array;
}
-+ (int) contactsCount
+- (int) contactsCount
{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+ //ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
return ABAddressBookGetPersonCount(addressBook);
}
-+ (int) contactsWithImageCount
+- (int) contactsWithImageCount
{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+ //ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
NSArray *peopleArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
int ncount = 0;
for (id person in peopleArray) if (ABPersonHasImageData(person)) ncount++;
@@ -44,9 +68,9 @@ + (int) contactsWithImageCount
return ncount;
}
-+ (int) contactsWithoutImageCount
+- (int) contactsWithoutImageCount
{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+ //ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
NSArray *peopleArray = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
int ncount = 0;
for (id person in peopleArray) if (!ABPersonHasImageData(person)) ncount++;
@@ -55,18 +79,17 @@ + (int) contactsWithoutImageCount
}
// Groups
-+ (int) numberOfGroups
-{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+- (int) numberOfGroups{
+ //ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
NSArray *groups = (NSArray *)ABAddressBookCopyArrayOfAllGroups(addressBook);
int ncount = groups.count;
[groups release];
return ncount;
}
-+ (NSArray *) groups
+- (NSArray *) groups
{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+ //ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
NSArray *groups = (NSArray *)ABAddressBookCopyArrayOfAllGroups(addressBook);
NSMutableArray *array = [NSMutableArray arrayWithCapacity:groups.count];
for (id group in groups)
@@ -76,7 +99,7 @@ + (NSArray *) groups
}
// Sorting
-+ (BOOL) firstNameSorting
+- (BOOL) firstNameSorting
{
return (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst);
}
@@ -84,32 +107,34 @@ + (BOOL) firstNameSorting
#pragma mark Contact Management
// Thanks to Eridius for suggestions re: error
-+ (BOOL) addContact: (ABContact *) aContact withError: (NSError **) error
+- (BOOL) addContact: (ABContact *) aContact withError: (NSError **) error
{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+ //ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
if (!ABAddressBookAddRecord(addressBook, aContact.record, (CFErrorRef *) error)) return NO;
- return ABAddressBookSave(addressBook, (CFErrorRef *) error);
+ //return ABAddressBookSave(addressBook, (CFErrorRef *) error);
+ return YES;
}
-+ (BOOL) addGroup: (ABGroup *) aGroup withError: (NSError **) error
+- (BOOL) addGroup: (ABGroup *) aGroup withError: (NSError **) error
{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+ //ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
if (!ABAddressBookAddRecord(addressBook, aGroup.record, (CFErrorRef *) error)) return NO;
return ABAddressBookSave(addressBook, (CFErrorRef *) error);
+ //return YES;
}
-+ (NSArray *) contactsMatchingName: (NSString *) fname
+- (NSArray *) contactsMatchingName: (NSString *) fname
{
NSPredicate *pred;
- NSArray *contacts = [ABContactsHelper contacts];
+ NSArray *contacts = [self contacts];
pred = [NSPredicate predicateWithFormat:@"firstname contains[cd] %@ OR lastname contains[cd] %@ OR nickname contains[cd] %@ OR middlename contains[cd] %@", fname, fname, fname, fname];
return [contacts filteredArrayUsingPredicate:pred];
}
-+ (NSArray *) contactsMatchingName: (NSString *) fname andName: (NSString *) lname
+- (NSArray *) contactsMatchingName: (NSString *) fname andName: (NSString *) lname
{
NSPredicate *pred;
- NSArray *contacts = [ABContactsHelper contacts];
+ NSArray *contacts = [self contacts];
pred = [NSPredicate predicateWithFormat:@"firstname contains[cd] %@ OR lastname contains[cd] %@ OR nickname contains[cd] %@ OR middlename contains[cd] %@", fname, fname, fname, fname];
contacts = [contacts filteredArrayUsingPredicate:pred];
pred = [NSPredicate predicateWithFormat:@"firstname contains[cd] %@ OR lastname contains[cd] %@ OR nickname contains[cd] %@ OR middlename contains[cd] %@", lname, lname, lname, lname];
@@ -117,19 +142,19 @@ + (NSArray *) contactsMatchingName: (NSString *) fname andName: (NSString *) lna
return contacts;
}
-+ (NSArray *) contactsMatchingPhone: (NSString *) number
+- (NSArray *) contactsMatchingPhone: (NSString *) number
{
NSPredicate *pred;
- NSArray *contacts = [ABContactsHelper contacts];
+ NSArray *contacts = [self contacts];
pred = [NSPredicate predicateWithFormat:@"phonenumbers contains[cd] %@", number];
return [contacts filteredArrayUsingPredicate:pred];
}
-+ (NSArray *) groupsMatchingName: (NSString *) fname
+- (NSArray *) groupsMatchingName: (NSString *) fname
{
NSPredicate *pred;
- NSArray *groups = [ABContactsHelper groups];
+ NSArray *groups = [self groups];
pred = [NSPredicate predicateWithFormat:@"name contains[cd] %@ ", fname];
return [groups filteredArrayUsingPredicate:pred];
}
-@end
+@end
View
11 ABGroup.m
@@ -24,9 +24,9 @@ + (id) groupWithRecord: (ABRecordRef) grouprec
return [[[ABGroup alloc] initWithRecord:grouprec] autorelease];
}
-+ (id) groupWithRecordID: (ABRecordID) recordID
-{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
++ (id) groupWithRecordID: (ABRecordID) recordID{
+ CFErrorRef error;
+ ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreateWithOptions(nil, &error));
ABRecordRef grouprec = ABAddressBookGetGroupWithRecordID(addressBook, recordID);
ABGroup *group = [self groupWithRecord:grouprec];
CFRelease(grouprec);
@@ -48,9 +48,8 @@ - (void) dealloc
[super dealloc];
}
-- (BOOL) removeSelfFromAddressBook: (NSError **) error
-{
- ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreate());
+- (BOOL) removeSelfFromAddressBook: (NSError **) error{
+ ABAddressBookRef addressBook = CFAutorelease(ABAddressBookCreateWithOptions(nil, (CFErrorRef *)error));
if (!ABAddressBookRemoveRecord(addressBook, self.record, (CFErrorRef *) error)) return NO;
return ABAddressBookSave(addressBook, (CFErrorRef *) error);
}
Something went wrong with that request. Please try again.