• Stars
    star
    698
  • Rank 64,841 (Top 2 %)
  • Language
    Objective-C
  • License
    Other
  • Created over 12 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Cocoa / Objective-C library for interfacing with the iOS AddressBook.

RHAddressBook

A Cocoa / Objective-C library for interfacing with the iOS AddressBook with added geocoding support.

  • All attributes on various objects are exposed as properties, allowing for simple Obj-C code. (No more dealing with CF methods etc )
  • Built in support for background Geocoding with an in-built persistent cache. (iOS5+ only)
  • vCard import and export for single and multiple people.
  • Access to all underlying ABRecordRefs & ABAddressBookRefs etc.
  • Maintains an underlying thread for each ab instance in-order to ensure thread safety.
  • Sends NSNotifications when ab has changed.
  • Geocoding is disabled by default. (See RH_AB_INCLUDE_GEOCODING)

Bonus Features

  • Unit Tests.
  • Basic Demo App.

Classes

  • RHAddressBook
  • RHSource - Representation of various address-book sources found on the iPhone
  • RHGroup
  • RHPerson - Represents a person in the addressbook.
  • RHMultiValue - Represents multiple key/value pairs. Used for RHPersons addresses etc.

Getting Started

Include RHAddressBook in your iOS project.

    #import <RHAddressBook/AddressBook.h>

Getting an instance of the addressbook.

    RHAddressBook *ab = [[[RHAddressBook alloc] init] autorelease];

Support for iOS6+ authorization

    //query current status, pre iOS6 always returns Authorized
    if ([RHAddressBook authorizationStatus] == RHAuthorizationStatusNotDetermined){
    
    	//request authorization
        [ab requestAuthorizationWithCompletion:^(bool granted, NSError *error) {
            [abViewController setAddressBook:ab];
        }];
    }

Registering for addressbook changes

    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(addressBookChanged:) name:RHAddressBookExternalChangeNotification object:nil];

Getting sources.

    NSArray *sources = [ab sources];
    RHSource *defaultSource = [ab defaultSource];

Getting a list of groups.

    NSArray *groups = [ab groups];
    long numberOfGroups = [ab numberOfGroups];
    NSArray *groupsInSource = [ab groupsInSource:defaultSource];
    RHGroup *lastGroup = [groups lastObject];

Getting a list of people.

    NSArray *allPeople = [ab people];
    long numberOfPeople = [ab numberOfPeople];
    NSArray *allPeopleSorted = [ab peopleOrderedByUsersPreference];
    NSArray *allFreds = [ab peopleWithName:@"Fred"];
    NSArray *allFredsInLastGroup = [lastGroup peopleWithName:@"Fred"];
    RHPerson *person = [allPeople lastObject];

Getting basic properties on on a person.

    NSString *department = [person department];
    UIImage *thumbnail = [person thumbnail];
    BOOL isCompany = [person isOrganization];

Setting basic properties on a person.

    person.name = @"Freddie";
    [person setImage:[UIImage imageNames:@"hahaha.jpg"]];
    person.kind = kABPersonKindOrganization;
    [person save];

Getting MultiValue properties on a person.

    RHMultiDictionaryValue *addressesMultiValue = [person addresses];
    NSString *firstAddressLabel = [RHPerson localizedLabel:[addressesMultiValue labelAtIndex]]; //eg Home
    NSDictionary *firstAddress = [addressesMultiValue valueAtIndex:0];

Setting MultiValue properties on a person.

    RHMultiStringValue *phoneMultiValue = [person phoneNumbers];
    RHMutableMultiStringValue *mutablePhoneMultiValue = [[phoneMultiValue mutableCopy] autorelease];
    if (! mutablePhoneMultiValue) mutablePhoneMultiValue = [[[RHMutableMultiStringValue alloc] initWithType:kABMultiStringPropertyType] autorelease];
    
    //RHPersonPhoneIPhoneLabel casts kABPersonPhoneIPhoneLabel to the correct toll free bridged type, see RHPersonLabels.h
    mutablePhoneMultiValue addValue:@"+14086655555" withLabel:RHPersonPhoneIPhoneLabel]; 
    person.phonenumbers = mutablePhoneMultiValue;
    [person save];

Creating a new person.

    RHPerson *newPerson = [[ab newPersonInDefaultSource] autorelease]; //added to ab
    RHPerson *newPerson2  = [[[RHPerson newPersonInSource:[ab defaultSource]] autorelease]; //not added to ab
    [ab addPerson:newPerson2];
    NSError* error = nil;
    if (![ab save:&error]) NSLog(@"error saving: %@", error);

Getting an RHPerson object for an ABRecordRef for editing. (note: RHPerson might not be associated with the same addressbook as the original ABRecordRef)

    ABRecordRef personRef = ...;
    RHPerson *person = [ab personForRecordRef:personRef];
    if(person){
        person.firstName = @"Paul";
        person.lastName = @"Frank";
        [person save];
    }

Presenting / editing an RHPerson instance in a ABPersonViewController.

    ABPersonViewController *personViewController = [[[ABPersonViewController alloc] init] autorelease];   

    //setup (tell the view controller to use our underlying address book instance, so our person object is directly updated on our behalf)
     [person.addressBook performAddressBookAction:^(ABAddressBookRef addressBookRef) {
        personViewController.addressBook =addressBookRef;
    } waitUntilDone:YES];

    personViewController.displayedPerson = person.recordRef;
    personViewController.allowsEditing = YES;

    [self.navigationController pushViewController:personViewController animated:YES];

Background geocoding

    if ([RHAddressBook isGeocodingSupported){
        [RHAddressBook setPreemptiveGeocodingEnabled:YES]; //class method
    }
    float progress = [_addressBook preemptiveGeocodingProgress]; // 0.0f - 1.0f

Geocoding results for a person.

    CLLocation *location = [person locationForAddressID:0];
    CLPlacemark *placemark = [person placemarkForAddressID:0];

Finding people within distance of a location.

    NSArray *inRangePeople = [ab peopleWithinDistance:5000 ofLocation:location];
    NSLog(@"people:%@", inRangePeople);

Saving. (all of the below are equivalent)

    BOOL changes = [ab hasUnsavedChanges];
    BOOL result = [ab save];
    BOOL result =[source save];
    BOOL result =[group save];
    BOOL result =[person save];

Reverting changes on objects. (reverts the entire addressbook instance, not just the object revert is called on.)

    [ab revert];
    [source revert];
    [group revert];
    [person revert];

Remember, save often in order to avoid painful save conflicts.

Installing

For instructions on how to get started using this static library see Using Static iOS Libraries at rheard.com.

Licence

Released under the Modified BSD License. (Attribution Required)

RHAddressBook

Copyright (c) 2011-2012 Richard Heard. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

iOS Version Support (Executive Summary: Supports iOS 4+, tested on iOS 4.0 - 7.1)

This Framework code runs and compiles on and has been tested all the way back to iOS 4.0.

Unit tests are in place that run on all versions between 4.0 and 7.1.

Various methods are not available when linking against older SDKs and will return nil when running on older os versions. eg. Geocoding is only supported on iOS 5+. You should always use the +[RHAddressBook isGeocodingAvailable] method to check whether geocoding is available before attempting to access geocode information. Methods will however, if available safely return nil / empty arrays.

More Repositories

1

RevealLoader

Reveal Loader dynamically loads libReveal.dylib (Reveal.app support) into iOS apps on jailbroken devices.
C
403
star
2

RHPreferences

A simple and easy Preferences window controller with multiple tabs for your next Mac application.
Objective-C
95
star
3

RHAdditions

Various Objective-C categories and additions that have served me well over the years.
Objective-C
81
star
4

RHHorizontalSwipe

A static library for iOS that allows for navigation inside a UIViewController by swiping left and right. It supports an arbitrary number of child view controllers as well as custom overlay views that are notified of changes to the underlying controller positions.
Objective-C
78
star
5

RHStatusItemView

An NSStatusItem hosted view that supports handling both left and right click actions, menus and showing an image / alternateImage pair.
Objective-C
59
star
6

ios-debugserver

LLDB debugserver for iOS with modified entitlements, allowing for debugging of any process.
41
star
7

Xiaomi-M365-Display

A BTLE display for your Xiaomi M365 Scooter, based on the Nordic Semiconductor nRF52832 SoC.
C
28
star
8

RHDisplayLinkStepper

A custom, CADisplayLink backed stepper, perfect for performing complex animations or firing delegate callbacks over a specified period of time.
Objective-C
26
star
9

RHIntervalTree

An Objective-C implementation of a centred interval tree.
Objective-C
17
star
10

RHKeychain

Simple wrapper functions to the OS X keychain to create, edit and delete generic password items.
Objective-C
13
star
11

BringBackDelete

A simple Safari 6.0+ extension that re-enables "back to previous page" navigation when you press the delete key.
JavaScript
12
star
12

RHFirstUserExperience

Easily load custom first user experience overlay views at runtime based on a plist of defined actions. Supports both Nib and Class based overlays. A first user experience manager for your next iOS project.
Objective-C
10
star
13

RHCoreLocationNotifier

Adds support to CLLocationManager for broadcasting location updates via NSNotification.
Objective-C
6
star
14

ANCServiceTypes

iOS Service Types for BTLE ANCS (Apple Notification Center Service)
C++
5
star
15

RHColorCoordinatedTableView

A UITableView subclass that provides preHeader and postFooter views positioned outside of the tableViews contentOffset allowing for custom header and footer background colours.
Objective-C
4
star
16

mixpanel-mac

Fork of the Mixpanel iOS Library to support Mac OS X Apps.
Objective-C
3
star
17

RHDynamicPropertyObject

RHDynamicPropertyObject makes it easy for subclasses provide dynamic implementations for properties at runtime.
Objective-C
3
star
18

Xcode4UserScripts

This collection of scripts allows for the running of arbitrary Xcode 3 User Scripts under Xcode 4. It includes Matt Gallagher's awesome PropertyFromInstanceVariable script via way of an example.
Perl
3
star
19

GeocoderDemo

Geocoding Sample App i wrote while at Apple
Objective-C
2
star
20

RHSQLiteKit

RHSQLiteKit wraps an instance of an SQLite.db file and provides an object based wrapper around the db's tables.
Objective-C
2
star
21

LayoutScrollView

LayoutScrollView has been moved to a static library and renamed to RHHorizontalSwipe. Find it at https://github.com/heardrwt/RHHorizontalSwipe
1
star