2 // ContactViewController.m
5 // Created by Quang Anh Do on 06/07/2010.
6 // Copyright 2010 Quang Anh Do. All rights reserved.
9 #import "ContactViewController.h"
10 #import "CommonFunctions.h"
12 @implementation ContactViewController
15 CFRelease(addressBook);
20 - (id)initWithId:(int)identifier {
21 NSDictionary *contactList = [NSKeyedUnarchiver unarchiveObjectWithFile:[applicationDocumentsDirectory() stringByAppendingPathComponent:@"contact_mapping.plist"]];
23 return [self initWithId:identifier
24 name:[contactList objectForKey:[NSString stringWithFormat:@"%d", identifier]]];
27 - (id)initWithId:(int)identifier name:(NSString *)name {
28 addressBook = ABAddressBookCreate();
30 ABRecordRef person = NULL;
31 CFArrayRef matches = ABAddressBookCopyPeopleWithName(addressBook, (CFStringRef)name);
33 if (matches && CFArrayGetCount(matches)) {
34 person = (id)CFArrayGetValueAtIndex(matches, 0);
36 ABMultiValueRef urls = ABRecordCopyValue(person, kABPersonURLProperty);
37 ABMutableMultiValueRef mutableURLs = NULL;
39 mutableURLs = ABMultiValueCreateMutableCopy(urls);
42 mutableURLs = ABMultiValueCreateMutable(kABStringPropertyType);
44 ABMultiValueAddValueAndLabel(mutableURLs, shortLinkToContact(identifier), CFSTR("ATutor"), NULL);
45 CFRelease(mutableURLs);
47 person = ABPersonCreate();
48 ABRecordSetValue(person, kABPersonFirstNameProperty, name, NULL);
50 ABMutableMultiValueRef urls = ABMultiValueCreateMutable(kABMultiStringPropertyType);
51 ABMultiValueAddValueAndLabel(urls, shortLinkToContact(identifier), CFSTR("ATutor"), NULL);
52 ABRecordSetValue(person, kABPersonURLProperty, urls, NULL);
54 [(id)person autorelease];
57 if (ABRecordGetRecordID(person) != kABRecordInvalidID) {
58 self = [[ABPersonViewController alloc] init];
59 [(ABPersonViewController *)self setAllowsEditing:YES];
60 [(ABPersonViewController *)self setDisplayedPerson:person];
62 self = [[ABUnknownPersonViewController alloc] init];
63 [(ABUnknownPersonViewController *)self setAllowsActions:YES];
64 [(ABUnknownPersonViewController *)self setAllowsAddingToAddressBook:YES];
65 [(ABUnknownPersonViewController *)self setDisplayedPerson:person];
68 if (matches) CFRelease(matches);