iOS开发获取通讯录的联系人以及发短信给联系人

2025-09-26 13:12:15

1、创建工程项目和视图控制器

      1、创建一个empty(空的)工程项目,新建一个UIViewController;

      2、选中工程,右键-New File…选择“Cocoa Touch Class”-Next,给个合理的名称LBAddressBookVC,再Next完成;

      3、在AppDelegate.m文件包含#import "LBAddressBookVC.h";

      4、初始化创建LBAddressBookVC的视图控制器,并用导航栏控制器包含。将之设置为根视图控制器。

iOS开发获取通讯录的联系人以及发短信给联系人

2、导入必须的库MessageUI.framework 、AddressBook.framework 、 AddressBookUI.framework

3、.h文件

#import <UIKit/UIKit.h>

@interface LBAddressBookVC : UIViewController <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, copy) NSMutableArray *personArray;

@end

4、.m文件

#import "LBAddressBookVC.h"

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h>

#import <MessageUI/MessageUI.h>

#define     jekPrompt                       @"提示"

#define     jekAddressBookMessage            @"来‘’找我吧"           //发短信 中的内容

@interface LBAddressBookVC ()

{

    

    NSInteger sectionNumber;

    NSInteger recordID;

    NSString *name;

    NSString *email;

    NSString *tel;

    NSMutableArray *addressBookTemp;

    

}

@property (nonatomic, assign)ABAddressBookRef addressBookRef;

@property NSInteger sectionNumber;

@property NSInteger recordID;

@property (nonatomic, retain) NSString *name;

@property (nonatomic, retain) NSString *email;

@property (nonatomic, retain) NSString *tel;

@end

@implementation LBAddressBookVC

@synthesize name,email,tel,recordID,sectionNumber,personArray;

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"通讯录中邀请";

    personArray = [NSMutableArray array];

    UITableView *tabview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

    tabview.delegate = self;

    tabview.dataSource = self;

    [self.view addSubview:tabview];

    [self getBook];

}

#pragma mark - 获取手机中的通讯录以及解析联系人

-(NSMutableArray *)getBook

{

    

    if (personArray == nil) {

         personArray = [NSMutableArray array];

    }

    //新建一个通讯录类

    ABAddressBookRef addressBooks = nil;

    

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)

        

    {

        addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);

        

        //获取通讯录权限

        

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

        

        

        

    }

    

    else

        

    {

        addressBooks = ABAddressBookCreate();

        

    }

    

    //获取通讯录中的所有人

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);

    

    //通讯录中人数

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);

    

    //循环,获取每个人的个人信息

    for (NSInteger i = 0; i < nPeople; i++)

    {

        //新建一个addressBook model类

        LBAddressBookVC *addressBook = [[LBAddressBookVC alloc] init];

        //获取个人

        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

        //获取个人名字

        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;

            }

            //获取电话号码和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);

        }

        //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息

        [personArray addObject:addressBook];

        

        if (abName) CFRelease(abName);

        if (abLastName) CFRelease(abLastName);

        if (abFullName) CFRelease(abFullName);

        

        NSLog(@"---%@",personArray);

    }

    

    return personArray;

}

#pragma mark - UITableView Delegate and Datasource functions

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return personArray.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.1;

}

- (CGFloat)tableView: (UITableView*)tableView heightForRowAtIndexPath: (NSIndexPath*) indexPath {

    return 52.5;

}

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

    }

    

    LBAddressBookVC *book = [personArray objectAtIndex:indexPath.row];

    

    cell.textLabel.text = book.name;

    

    cell.detailTextLabel.text = book.tel;

    

    

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    LBAddressBookVC *book = [personArray objectAtIndex:indexPath.row];

   

    NSString * phoneNumber = [book.tel stringByReplacingOccurrencesOfString:@"-" withString:@""];

    [self sendSmsMessageWithPhoneNumber:phoneNumber];

}

#pragma mark - 短信

-(void)sendSmsMessageWithPhoneNumber:(NSString *)phoneNumber

{

    if([MFMessageComposeViewController canSendText])

    {

        [self displaySMSComposerSheetPhoneNumber:phoneNumber];

    }

    else

    {

        UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:jekPrompt message:@"该设备不支持发短信" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

        [msgbox show];

    }

}

-(void)displaySMSComposerSheetPhoneNumber:(NSString *)phoneNumber

{

    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];

    picker.messageComposeDelegate = (id<MFMessageComposeViewControllerDelegate>)self;

    

    picker.recipients = [NSArray arrayWithObject:phoneNumber];

    

    picker.body=[NSString stringWithFormat:@"%@",jekAddressBookMessage];

    

    [self presentViewController:picker animated:YES completion:nil];

    

}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller

                 didFinishWithResult:(MessageComposeResult)result {

    

    switch (result)

    {

        case MessageComposeResultCancelled:

            NSLog(@"Result: SMS sending canceled");

            break;

        case MessageComposeResultSent:

            NSLog(@"Result: SMS sent");

            break;

        case MessageComposeResultFailed:

        {

            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:jekPrompt message:@"短信发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

            

            [msgbox show];

            

        }

            break;

        default:

            NSLog(@"Result: SMS not sent");

            break;

    }

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end

iOS开发获取通讯录的联系人以及发短信给联系人

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢