Quantcast
Channel: iPhone Tutorial | iPhone iOS4 iPad SDK Development & Programming Blog » ios6
Viewing all articles
Browse latest Browse all 5

Address Book tutorial for iPhone – Part 2

$
0
0

We create a new method getPersonOutOfAddressBook where our most important actions take place.

- (void)getPersonOutOfAddressBook 
{     
  CFErrorRef error = NULL;          

 ABAddressBookRef addressBook = 
ABAddressBookCreateWithOptions(NULL, &error);          

if (addressBook != nil)     
{       
  NSLog(@"Succesful.");                  

NSArray *allContacts = (__bridge_transfer NSArray 
*)ABAddressBookCopyArrayOfAllPeople(addressBook);                  
NSUInteger i = 0;         
for (i = 0; i < [allContacts count]; i++)         
{       
      Person *person = [[Person alloc] init];  
                        
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];                          

NSString *firstName = (__bridge_transfer NSString 
*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);             
NSString *lastName =  (__bridge_transfer NSString 
*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);             
NSString *fullName = [NSString stringWithFormat:@"%@ %@", 
firstName, lastName];                         

 person.firstName = firstName;            
 person.lastName = lastName;            
 person.fullName = fullName;                       

   //email             
ABMultiValueRef emails = ABRecordCopyValue(contactPerson, 
kABPersonEmailProperty);                          
NSUInteger j = 0;             
for (j = 0; j < ABMultiValueGetCount(emails); j++)           
  {                 
NSString *email = (__bridge_transfer NSString 
*)ABMultiValueCopyValueAtIndex(emails, j);                 
if (j == 0)              
   {                
     person.homeEmail = email;                 
NSLog(@"person.homeEmail = %@ ", person.homeEmail);           
      }                           
       else if (j==1)           
          person.workEmail = email;          
   }                       
   [self.tableData addObject:person];       
  }     
}        
  CFRelease(addressBook); 
}



A lot is going on here in this method. Let’s walk through the different parts

1. In case of an error we create an error object. We get a reference to the user’s address book.

2. we copy all contacts from the address book in an array.

3. We loop through the contacts in the address book. For each contact, we create a Person Object.

4. We copy the first name and second name properties to a NSString and concatenate the strings to the full name, which we will display in our table view later on. The names are assigned to the Person object.

5. The email property of the address book is assigned to a multi value reference because there are multiple emails we loop through each of them and we assign them to the Person.object

6. Each person object is added to the tableData array

7. When we are done with the address book it can be released

8. When something went wrong the error is logged.

Now Build and Run the code, you should see all Contact’s full names in the table view.

Now let’s create the contacts view. Add a new File. Name it ContactViewController and make it a subclass of ViewController. Also check the “With XIB for user interface” checkbox. Open ContactViewController.xib and drag the following Labels to the view so it looks like this.

Now open ContactViewController.h file and modify it as follows.

#import <UIKit/UIKit.h> 
#import "Person.h"  

@interface ContactViewController : UIViewController  
@property (nonatomic, strong) Person *person; 
@property (nonatomic, strong) NSString *firstName; 
@property (nonatomic, strong) NSString *lastName; 
@property (nonatomic, strong) NSString *homeEmail; 
@property (nonatomic, strong) NSString *workEmail; 
 - (id)initWithPerson:(Person *)person; 
 @end

In contactViewController.m add the following outlet properties.

@interface ContactViewController ()  
@property (nonatomic, strong) IBOutlet UILabel *firstNameLabel; 
@property (nonatomic, strong) IBOutlet UILabel *lastNameLabel; 
@property (nonatomic, strong) IBOutlet UILabel *homeEmailLabel; 
@property (nonatomic, strong) IBOutlet UILabel *workEmailLabel; 
 @end


Implement the initWithPerson method. This method will be called when the view will be created. The data from our Person Object will be assigned to the ContactViewController’s properties.

- (id)initWithPerson:(Person *)person 
{    
 self = [super initWithNibName:@"ContactViewController" bundle:nil];   
  if (self) {      
   _firstName = [person.firstName copy];     
   _lastName = [person.lastName copy];      
   _homeEmail = [person.homeEmail copy];     
   _workEmail = [person.workEmail copy];   
  }   
  return self; 
}



Go back to ContactViewController.xib and make the following connections.

In ViewController.m import the ContactViewController also implement the tableview:didSelectRowAtIndexPath as follows.

#import "ContactViewController.h" 
 - (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {    
 Person *person = [self.tableData objectAtIndex:indexPath.row];   

   ContactViewController *contactViewController = [[ContactViewController 
alloc] initWithPerson:person];    
 [self.navigationController pushViewController:contactViewController 
animated:YES];
 }



When the users select a User’s name in the table view, the contactViewController is initialized and the person object is sent along. The contactViewController is then pushed on the navigation’s stack. Now Build and Run the code, when you select a name you should see the following view.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles



Latest Images