最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS開(kāi)發(fā)中使app獲取本機(jī)通訊錄的實(shí)現(xiàn)代碼實(shí)例

 更新時(shí)間:2016年01月19日 09:15:32   作者:芳草小腳丫  
這篇文章主要介紹了iOS開(kāi)發(fā)中使app獲取本機(jī)通訊錄的實(shí)現(xiàn)代碼實(shí)例,主要用到了AddressBook.framework和AddressBookUI.framework,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

一、在工程中添加AddressBook.framework和AddressBookUI.framework

二、獲取通訊錄

1、在infterface中定義數(shù)組并在init方法中初始化

復(fù)制代碼 代碼如下:

NSMutableArray *addressBookTemp;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    addressBookTemp = [NSMutableArray array];
}

2、定義一個(gè)model,用來(lái)存放通訊錄中的各個(gè)屬性
新建一個(gè)繼承自NSObject的類(lèi),在.h中
復(fù)制代碼 代碼如下:

@interface TKAddressBook : NSObject {
    NSInteger sectionNumber;
    NSInteger recordID;
    NSString *name;
    NSString *email;
    NSString *tel;
}
@property NSInteger sectionNumber;
@property NSInteger recordID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *tel;
 
@end

在.m文件中進(jìn)行synthesize
復(fù)制代碼 代碼如下:

@implementation TKAddressBook
@synthesize name, email, tel, recordID, sectionNumber;
 
@end

3、獲取聯(lián)系人

在iOS6之后,獲取通訊錄需要獲得權(quán)限

復(fù)制代碼 代碼如下:

    //新建一個(gè)通訊錄類(lèi)
    ABAddressBookRef addressBooks = nil;
 
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
 
    {
        addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);
 
        //獲取通訊錄權(quán)限
 
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
 
        ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});
 
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
 
        dispatch_release(sema);
 
    }
 
    else
 
    {
        addressBooks = ABAddressBookCreate();
 
    }
 
//獲取通訊錄中的所有人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);

//通訊錄中人數(shù)
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
 
//循環(huán),獲取每個(gè)人的個(gè)人信息
for (NSInteger i = 0; i < nPeople; i++)
    {
        //新建一個(gè)addressBook model類(lèi)
        TKAddressBook *addressBook = [[TKAddressBook alloc] init];
        //獲取個(gè)人
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
        //獲取個(gè)人名字
        CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
        CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
        CFStringRef abFullName = ABRecordCopyCompositeName(person);
        NSString *nameString = (__bridge NSString *)abName;
        NSString *lastNameString = (__bridge NSString *)abLastName;
        
        if ((__bridge id)abFullName != nil) {
            nameString = (__bridge NSString *)abFullName;
        } else {
            if ((__bridge id)abLastName != nil)
            {
                nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];
            }
        }
        addressBook.name = nameString;
        addressBook.recordID = (int)ABRecordGetRecordID(person);;
        
        ABPropertyID multiProperties[] = {
            kABPersonPhoneProperty,
            kABPersonEmailProperty
        };
        NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);
        for (NSInteger j = 0; j < multiPropertiesTotal; j++) {
            ABPropertyID property = multiProperties[j];
            ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);
            NSInteger valuesCount = 0;
            if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);
            
            if (valuesCount == 0) {
                CFRelease(valuesRef);
                continue;
            }
            //獲取電話號(hào)碼和email
            for (NSInteger k = 0; k < valuesCount; k++) {
                CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
                switch (j) {
                    case 0: {// Phone number
                        addressBook.tel = (__bridge NSString*)value;
                        break;
                    }
                    case 1: {// Email
                        addressBook.email = (__bridge NSString*)value;
                        break;
                    }
                }
                CFRelease(value);
            }
            CFRelease(valuesRef);
        }
        //將個(gè)人信息添加到數(shù)組中,循環(huán)完成后addressBookTemp中包含所有聯(lián)系人的信息
        [addressBookTemp addObject:addressBook];
        
        if (abName) CFRelease(abName);
        if (abLastName) CFRelease(abLastName);
        if (abFullName) CFRelease(abFullName);
    }


三、顯示在table中
復(fù)制代碼 代碼如下:

//行數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
 
//列數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [addressBookTemp count];
}

//cell內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellIdentifier = @"ContactCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
 
    TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];
 
    cell.textLabel.text = book.name;
 
    cell.detailTextLabel.text = book.tel;
 
    return cell;
}


列表效果

201611991316031.png (314×132)

PS:通訊錄中電話號(hào)碼中的"-"可以在存入數(shù)組之前進(jìn)行處理,屬于NSString處理的范疇,解決辦法有很多種,本文不多加說(shuō)明

四、刪除通訊錄數(shù)據(jù)

復(fù)制代碼 代碼如下:

<span style="white-space:pre">    </span>ABRecordRef person = objc_unretainedPointer([myContacts objectAtIndex:indexPath.row]); 
        CFErrorRef *error; 
        ABAddressBookRemoveRecord(addressBook, person, error); 
        ABAddressBookSave(addressBook, error); 
        myContacts = nil; 
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

相關(guān)文章

最新評(píng)論

汾西县| 开江县| 肇源县| 张家口市| 桐庐县| 马尔康县| 托里县| 清流县| 湖北省| 天等县| 施秉县| 扎囊县| 白玉县| 临颍县| 天长市| 平陆县| 根河市| 镇原县| 晴隆县| 德安县| 巨鹿县| 英超| 读书| 泉州市| 富源县| 贡嘎县| 嵊泗县| 体育| 晋江市| 茶陵县| 清河县| 西平县| 中阳县| 五大连池市| 伽师县| 新密市| 会泽县| 镇巴县| 津南区| 宜阳县| 苗栗县|