RHAddressBook 1.1.1

RHAddressBook 1.1.1

测试已测试
语言语言 Objective CObjective C
许可证 BSD
发布最后发布2014年12月

Richard Heard 维护。



  • 作者
  • Richard Heard

A Cocoa / Objective-C 库,用于与 iOS AddressBook 接口,并添加了地理编码支持。

  • 将各种对象的所有属性都公开为属性,允许简单的 Objective-C 代码(不再需要与 CF 方法等交互)。
  • 内置对背景地理编码的支持和内置的持久缓存。(仅限 iOS5+)
  • 单人和多人 vCard 导入和导出。
  • 访问所有底层 ABRecordRefs & ABAddressBookRefs 等。
  • 为每个 ab 实例维护一个线程以确保线程安全。
  • 当 ab 发生变化时发送 NSNotifications。
  • 默认禁用地理编码。(见 RH_AB_INCLUDE_GEOCODING)

额外功能

  • 单元测试。
  • 基本演示应用程序。

  • RHAddressBook
  • RHSource - 表示在 iPhone 上找到的各种地址簿来源
  • RHGroup
  • RHPerson - 代表地址簿中的一人。
  • RHMultiValue - 代表多个键/值对。用于 RHPersons 地址等。

入门

在您的 iOS 项目中包含 RHAddressBook。

    #import <RHAddressBook/AddressBook.h>

获取地址簿的实例。

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

支持 iOS6+ 授权。

    //query current status, pre iOS6 always returns Authorized
    if ([RHAddressBook authorizationStatus] == RHAuthorizationStatusNotDetermined){

        //request authorization
        [ab requestAuthorizationWithCompletion:^(bool granted, NSError *error) {
            [abViewController setAddressBook:ab];
        }];
    }

订阅地址簿更改。

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

获取来源。

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

获取组列表。

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

获取人员列表。

    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];

获取一个人的基本属性。

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

设置一个人的基本属性。

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

获取一个人的 MultiValue 属性。

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

设置一个人的 MultiValue 属性。

    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];

创建一个新的人。

    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);

获取一个用于编辑的 ABRecordRef 的 RHPerson 对象。 (注意:RHPerson 可能不关联与原始 ABRecordRef 相同的地址簿)

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

在 ABPersonViewController 中呈现/编辑 RHPerson 实例。

    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];

后台地理编码。

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

一个人的地理编码结果。

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

查找位于某位置附近的人。

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

保存。以下所有操作都是等效的

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

撤销对象上的更改。 (撤销整个地址簿实例,而不仅仅是调用撤销操作的对象。)

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

记住,为了防止痛苦的保存冲突,请经常保存。

安装

有关如何开始使用此静态库的说明,请参阅 rheard.com 上的 Using Static iOS Libraries

许可证

在修改后的BSD许可证下发布。(需署名)

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版本支持(摘要:支持iOS 4+,已在iOS 4.0-7.1上测试)

此框架代码在iOS 4.0及更高版本上运行和编译,并已进行过测试。

已经实施单元测试,这些测试在4.0和7.1之间的所有版本上都可运行。

当在较旧的SDK上进行链接时,某些方法不可用,在较旧的操作系统版本上运行时会返回nil。例如,地理编码只在iOS 5+上受支持。您应该在尝试访问地理编码信息之前,始终使用+ [RHAddressBook isGeocodingAvailable]方法来检查地理编码是否可用。但是,如果方法可用,它们会安全地返回nil/空数组。