Top Banner
CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App 1 Tuesday, February 16, 2010
89

CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Jun 25, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CS193P - Lecture 13iPhone Application Development

Address Book - Putting People in Your App

1Tuesday, February 16, 2010

Page 2: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Announcements• Paparazzi 3 due tomorrow at 11:59PM• Paparazzi 4 (last assignment!) due next Wednesday

2Tuesday, February 16, 2010

Page 3: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Final Project Proposals• Due tomorrow night!

■ Handout on website has all the info

• If you still need an idea for a project, let us know• We will be responding with feedback & a thumbs-up

3Tuesday, February 16, 2010

Page 4: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Address Book APIs• CoreFoundation• Merging from an external source of people• Using contacts in your application

Today’s Topics

4Tuesday, February 16, 2010

Page 5: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Putting Contacts in Your AppThe Hello World of Address Book

5Tuesday, February 16, 2010

Page 6: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

The Hello World of Address Book• Create a person and set some properties• Create ABPersonViewController• Push the view controller onto the navigation stack

6Tuesday, February 16, 2010

Page 7: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation

7Tuesday, February 16, 2010

Page 8: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation vs. Foundation

8Tuesday, February 16, 2010

Page 9: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation vs. Foundation• CoreFoundation is a framework written in C

8Tuesday, February 16, 2010

Page 10: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation vs. Foundation• CoreFoundation is a framework written in C• Many parallels to Foundation

■ CFDictionaryRef, CFStringRef■ CFRetain, CFRelease

8Tuesday, February 16, 2010

Page 11: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation vs. Foundation• CoreFoundation is a framework written in C• Many parallels to Foundation

■ CFDictionaryRef, CFStringRef■ CFRetain, CFRelease

• AddressBook framework is also C-based■ Uses CoreFoundation data types and semantics

8Tuesday, February 16, 2010

Page 12: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation vs. Foundation• CoreFoundation is a framework written in C• Many parallels to Foundation

■ CFDictionaryRef, CFStringRef■ CFRetain, CFRelease

• AddressBook framework is also C-based■ Uses CoreFoundation data types and semantics

• Addition to memory management naming conventions■ Functions with Create in their title return a retained object■ For example, ABAddressBookCreate( );

8Tuesday, February 16, 2010

Page 13: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Toll-Free Bridging• Supported for many types of objects

■ Strings, arrays, dictionaries, dates, numbers, data streams, more

• Use an NSString* where a CFStringRef is expected & vice versa• Very convenient for mixing & matching C with Objective-C

9Tuesday, February 16, 2010

Page 14: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Toll-Free Bridging• Supported for many types of objects

■ Strings, arrays, dictionaries, dates, numbers, data streams, more

• Use an NSString* where a CFStringRef is expected & vice versa• Very convenient for mixing & matching C with Objective-C

CFArrayRef array = ABAddressBookCopyPeopleWithName(...);

NSLog(@“%d”, [(NSArray *)array count]);

NSMutableArray *mutableArray = [(NSArray *)array mutableCopy];[mutableArray release];

if (array) {CFRelease(array);

}

9Tuesday, February 16, 2010

Page 15: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation and NULL

10Tuesday, February 16, 2010

Page 16: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation and NULL• Unlike Objective-C, must NULL-check CF type objects

10Tuesday, February 16, 2010

Page 17: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation and NULL• Unlike Objective-C, must NULL-check CF type objects

■ (Since nil is typed id, we use NULL for CF)

10Tuesday, February 16, 2010

Page 18: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation and NULL• Unlike Objective-C, must NULL-check CF type objects

■ (Since nil is typed id, we use NULL for CF)

CFStringRef string = CreateSomeCFString...;if (string != NULL) {

DoSomethingWith(string);CFRelease(string);

}

10Tuesday, February 16, 2010

Page 19: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation and NULL• Unlike Objective-C, must NULL-check CF type objects

■ (Since nil is typed id, we use NULL for CF)

■ Toll-free bridging can make this easier

CFStringRef string = CreateSomeCFString...;if (string != NULL) {

DoSomethingWith(string);CFRelease(string);

}

10Tuesday, February 16, 2010

Page 20: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

CoreFoundation and NULL• Unlike Objective-C, must NULL-check CF type objects

■ (Since nil is typed id, we use NULL for CF)

■ Toll-free bridging can make this easier

CFStringRef string = CreateSomeCFString...;if (string != NULL) {

DoSomethingWith(string);CFRelease(string);

}

NSString *string = (NSString *)CreateSomeCFString...;NSLog(@“%@”, [string lowercaseString]);[string autorelease]; // Even use autorelease!

10Tuesday, February 16, 2010

Page 21: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Beyond Hello World

11Tuesday, February 16, 2010

Page 22: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Social Networking Website

12Tuesday, February 16, 2010

Page 23: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Social Networking Website• People on the web

12Tuesday, February 16, 2010

Page 24: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Social Networking Website• People on the web• People on the iPhone

12Tuesday, February 16, 2010

Page 25: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Social Networking Website• People on the web• People on the iPhone• Reconciling them

12Tuesday, February 16, 2010

Page 26: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What Do We Need to Do?

13Tuesday, February 16, 2010

Page 27: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What Do We Need to Do?• Download

13Tuesday, February 16, 2010

Page 28: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What Do We Need to Do?• Download

• Search

13Tuesday, February 16, 2010

Page 29: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What Do We Need to Do?• Download

• Search

• Update

13Tuesday, February 16, 2010

Page 30: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What Do We Need to Do?• Download

• Search

• Update

• Display

13Tuesday, February 16, 2010

Page 31: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Search• Get the address book• Search the people

Step One

14Tuesday, February 16, 2010

Page 32: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Search• Get the address book• Search the people

ABAddressBookRef ab = ABAddressBookCreate();CFArrayRef people = ABAddressBookCopyPeopleWithName(ab, name);

Step One

14Tuesday, February 16, 2010

Page 33: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Address Book• ABAddressBookRef• Gives you access to the people• Central point for all things address book• Multiple instances, a single database

ABAddressBookRef ab = ABAddressBookCreate();

15Tuesday, February 16, 2010

Page 34: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Person• ABRecordRef• A collection of properties

■ First and last name■ Image■ Phone numbers, emails, etc…

16Tuesday, February 16, 2010

Page 35: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Properties can have different types■ String■ Date■ Dictionary, Data…

• Some properties may have multiple values■ Telephone: home, work, mobile, fax…

• Person properties in ABPerson.h

Properties

17Tuesday, February 16, 2010

Page 36: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• First Name, last name, birthday, etc…

• CoreFoundation types

Single Value Properties

18Tuesday, February 16, 2010

Page 37: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• First Name, last name, birthday, etc…

• CoreFoundation types

• Retrieve values with ABRecordCopyValue(…)

Single Value Properties

18Tuesday, February 16, 2010

Page 38: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• First Name, last name, birthday, etc…

• CoreFoundation types

• Retrieve values with ABRecordCopyValue(…)

Single Value Properties

CFStringRef first = ABRecordCopyValue(person, kABPersonFirstNameProperty);

18Tuesday, February 16, 2010

Page 39: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• First Name, last name, birthday, etc…

• CoreFoundation types

• Retrieve values with ABRecordCopyValue(…)

• Set values with ABRecordSetValue(…)

Single Value Properties

CFStringRef first = ABRecordCopyValue(person, kABPersonFirstNameProperty);

18Tuesday, February 16, 2010

Page 40: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• First Name, last name, birthday, etc…

• CoreFoundation types

• Retrieve values with ABRecordCopyValue(…)

• Set values with ABRecordSetValue(…)

Single Value Properties

CFStringRef first = ABRecordCopyValue(person, kABPersonFirstNameProperty);

CFDateRef date = CFDateCreate(…)ABRecordSetValue(person, kABPersonBirthdayProperty, date, &error);

18Tuesday, February 16, 2010

Page 41: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Phones, emails, URLs, etc…

• Access just like single value properties

• ABMultiValueRef

• Container for values and labels

Multi Value Properties

label: kABHomeLabelvalue: (408) 555 1234

label: kABWorkLabelvalue: (408) 555 2345

ABMultiValueRefproperty:kABPersonPhoneProperty

label: kABHomeLabelvalue: (415) 555 2375

19Tuesday, February 16, 2010

Page 42: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

ABMultiValueRef

20Tuesday, February 16, 2010

Page 43: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Count

ABMultiValueRef

20Tuesday, February 16, 2010

Page 44: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Count

ABMultiValueRef

CFIndex count = ABMultiValueGetCount(multiValue);

20Tuesday, February 16, 2010

Page 45: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Count

• Value

ABMultiValueRef

CFIndex count = ABMultiValueGetCount(multiValue);

20Tuesday, February 16, 2010

Page 46: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Count

• Value

ABMultiValueRef

CFIndex count = ABMultiValueGetCount(multiValue);

CFTypeRef value = ABMultiValueCopyValueAtIndex(mv, index);

20Tuesday, February 16, 2010

Page 47: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Count

• Value

• Label

ABMultiValueRef

CFIndex count = ABMultiValueGetCount(multiValue);

CFTypeRef value = ABMultiValueCopyValueAtIndex(mv, index);

20Tuesday, February 16, 2010

Page 48: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Count

• Value

• Label

ABMultiValueRef

CFIndex count = ABMultiValueGetCount(multiValue);

CFTypeRef value = ABMultiValueCopyValueAtIndex(mv, index);

CFStringRef label = ABMultiValueCopyLabelAtIndex(mv, index);

20Tuesday, February 16, 2010

Page 49: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Count

• Value

• Label

• Identifier

ABMultiValueRef

CFIndex count = ABMultiValueGetCount(multiValue);

CFTypeRef value = ABMultiValueCopyValueAtIndex(mv, index);

CFStringRef label = ABMultiValueCopyLabelAtIndex(mv, index);

20Tuesday, February 16, 2010

Page 50: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

• Count

• Value

• Label

• Identifier

ABMultiValueRef

CFIndex count = ABMultiValueGetCount(multiValue);

CFTypeRef value = ABMultiValueCopyValueAtIndex(mv, index);

CFStringRef label = ABMultiValueCopyLabelAtIndex(mv, index);

CFIndex identifier = ABMultiValueGetIdentifierAtIndex(mv, index);

20Tuesday, February 16, 2010

Page 51: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Update• Mutate the multi value• Add the value• Set the value on the person• Save the Address Book

Step Two

21Tuesday, February 16, 2010

Page 52: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Update• Mutate the multi value• Add the value• Set the value on the person• Save the Address Book

ABMultiValueRef urls = ABRecordCopyValue(person, kABPersonURLProperty);

ABMutableMultiValueRef urlCopy = ABMultiValueCreateMutableCopy(urls);ABMultiValueAddValueAndLabel(urlCopy, "the url", "social", NULL);ABRecordSetValue(person, urlCopy, kABPersonURLProperty);

ABAddressBookSave(ab, &err);

Step Two

21Tuesday, February 16, 2010

Page 53: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Display• Sort• Get the name• Display

Step Three

22Tuesday, February 16, 2010

Page 54: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Sorting• We’ll do it for you• ABPersonGetSortOrdering• ABPersonComparePeopleByName

23Tuesday, February 16, 2010

Page 55: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Sorting• We’ll do it for you• ABPersonGetSortOrdering• ABPersonComparePeopleByName

CFMutableArrayRef people = // obtain an array of peopleCFRange fullRange = CFRangeMake(0, CFArrayGetCount(people));

ABPersonSortOrdering sortOrdering = ABPersonGetSortOrdering();

CFArraySortValues(people, fullRange, ABPersonComparePeopleByName, (void*)sortOrdering);

23Tuesday, February 16, 2010

Page 56: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Sorting• We’ll do it for you• ABPersonGetSortOrdering• ABPersonComparePeopleByName

CFMutableArrayRef people = // obtain an array of peopleCFRange fullRange = CFRangeMake(0, CFArrayGetCount(people));

ABPersonSortOrdering sortOrdering = ABPersonGetSortOrdering();

CFArraySortValues(people, fullRange, ABPersonComparePeopleByName, (void*)sortOrdering);

// Objective-C alternative[people sortUsingFunction:ABPersonComparePeopleByName context:(void*)sortOrdering];

23Tuesday, February 16, 2010

Page 57: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Getting the Name• ABRecordCopyCompositeName

24Tuesday, February 16, 2010

Page 58: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Getting the Name• ABRecordCopyCompositeName

ABRecordRef person = // get a personCFStringRef name = ABRecordCopyCompositeName(person);

// do something clever with that person’s name

24Tuesday, February 16, 2010

Page 59: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Getting the Name• ABRecordCopyCompositeName

ABRecordRef person = // get a personCFStringRef name = ABRecordCopyCompositeName(person);

// do something clever with that person’s name

ABRecordRef person = // get a personNSString *name = (NSString*)ABRecordCopyCompositeName(person);

// do something clever with that person’s name

24Tuesday, February 16, 2010

Page 60: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

DemoBringing the people to the phone

25Tuesday, February 16, 2010

Page 61: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What We Just Saw• Searching for people by name• Using multi values• Sorting and Displaying people

26Tuesday, February 16, 2010

Page 62: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Showing Detailed Information

27Tuesday, February 16, 2010

Page 63: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Person View Controller• ABPersonViewController

■ displayedPerson■ displayedProperties■ allowsEditing

28Tuesday, February 16, 2010

Page 64: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Adding Contacts to Address Book

29Tuesday, February 16, 2010

Page 65: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Unknown Person View Controller• ABUnknownPersonViewController

■ displayedPerson■ allowsAddingToAddressBook■ delegate

30Tuesday, February 16, 2010

Page 66: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Unknown Person View Controller• ABUnknownPersonViewController

■ displayedPerson■ allowsAddingToAddressBook■ delegate

- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(ABRecordRef)person {

// do something

}

30Tuesday, February 16, 2010

Page 67: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

DemoShowing the people

31Tuesday, February 16, 2010

Page 68: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What Did We Just See?• Display contacts with ABPersonViewController• Add to Address Book with ABUnknownPersonViewController

32Tuesday, February 16, 2010

Page 69: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What If It Takes Too Long?• Doing things in the background

■ NSThread■ pthread

33Tuesday, February 16, 2010

Page 70: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Threading Model• Each thread needs its own ABAddressBookRef• What you can pass between threads:

■ Values■ ABRecordID

34Tuesday, February 16, 2010

Page 71: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Using the PeopleAdding people to an existing application

35Tuesday, February 16, 2010

Page 72: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Pick an email address and send an email• Create an email button• Pick a person• Build an email URL and open it

36Tuesday, February 16, 2010

Page 73: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Picking People• ABPeoplePickerNavigationController

37Tuesday, February 16, 2010

Page 74: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Picking People• ABPeoplePickerNavigationController

37Tuesday, February 16, 2010

Page 75: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Picking People• ABPeoplePickerNavigationController

37Tuesday, February 16, 2010

Page 76: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

ABPeoplePickerNavigationController• Present the Navigation Controller• ABPeoplePickerNavigationControllerDelegate

■ Cancellation■ Selection of a person■ Selection of a value

38Tuesday, February 16, 2010

Page 77: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

DemoBecause my friends like to receive emails

39Tuesday, February 16, 2010

Page 78: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What We Just Saw• Present ABPeoplePickerNavigationController modally• Delegate callbacks

40Tuesday, February 16, 2010

Page 79: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

People Storage• Get the record identifier• Serialize and deserialize it• Look it up in Address Book

41Tuesday, February 16, 2010

Page 80: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

People Storage• Get the record identifier• Serialize and deserialize it• Look it up in Address Book

ABRecordID personID = ABRecordGetRecordID(person);NSNumber *personIDAsNumber = [NSNumber numberWithInt:personID];

// serialize the NSNumber

41Tuesday, February 16, 2010

Page 81: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

People Storage• Get the record identifier• Serialize and deserialize it• Look it up in Address Book

ABRecordID personID = ABRecordGetRecordID(person);NSNumber *personIDAsNumber = [NSNumber numberWithInt:personID];

// serialize the NSNumber

NSNumber *personIDAsNumber = // Deserialize the NSNumberABRecordID personID = [personIDAsNumber intValue];

ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab, personID);

41Tuesday, February 16, 2010

Page 82: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What if the Database Changed?• Check before displaying• Store extra information

42Tuesday, February 16, 2010

Page 83: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

What if the Database Changed?• Check before displaying• Store extra information

ABRecordID recordID = // get the record

ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab, personID);

if (person != NULL) { // use the person} else { // fallback to other data}

42Tuesday, February 16, 2010

Page 84: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Notifications• Register callback

■ ABAddressBookRegisterExternalChangeCallback■ C callback

• And then what?■ Revert to get the changes■ Update the user interface

43Tuesday, February 16, 2010

Page 85: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Notifications• Register callback

■ ABAddressBookRegisterExternalChangeCallback■ C callback

• And then what?■ Revert to get the changes■ Update the user interface

// Recipe Detail View ControllerABAddressBookRegisterExternalChangeCallback(ab, abChanged, self);

43Tuesday, February 16, 2010

Page 86: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Notifications• Register callback

■ ABAddressBookRegisterExternalChangeCallback■ C callback

• And then what?■ Revert to get the changes■ Update the user interface

// Recipe Detail View ControllerABAddressBookRegisterExternalChangeCallback(ab, abChanged, self);

void abChanged(ABAddressBookRef ab, CFDictionaryRef info, void *context) { ABAddressBookRevert(ab); [(RecipeDetailViewController*)context reloadData];}

43Tuesday, February 16, 2010

Page 87: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

More on Change Callbacks• Revert or ignore the changes• Threading and change callbacks

44Tuesday, February 16, 2010

Page 88: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Summary• Low level C API for dealing with people• View controllers for presenting the people

45Tuesday, February 16, 2010

Page 89: CS193P - Lecture 13 · CS193P - Lecture 13 iPhone Application Development Address Book - Putting People in Your App Tuesday, February 16, 2010 1

Questions?

46Tuesday, February 16, 2010