iOS开发获取通讯录的联系人以及发短信给联系人
来源:网络收集 点击: 时间:2024-07-15创建工程项目和视图控制器
1、创建一个empty(空的)工程项目,新建一个UIViewController;
2、选中工程,右键-New File…选择“Cocoa Touch Class”-Next,给个合理的名称LBAddressBookVC,再Next完成;
3、在AppDelegate.m文件包含#import LBAddressBookVC.h;
4、初始化创建LBAddressBookVC的视图控制器,并用导航栏控制器包含。将之设置为根视图控制器。

导入必须的库MessageUI.framework 、AddressBook.framework 、AddressBookUI.framework
3/4.h文件
#import UIKit/UIKit.h
@interface LBAddressBookVC : UIViewController UITableViewDelegate,UITableViewDataSource
@property (nonatomic, copy) NSMutableArray *personArray;
@end
4/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 {
;
self.title = @通讯录中邀请;
personArray = ;
UITableView *tabview = initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
tabview.delegate = self;
tabview.dataSource = self;
;
;
}
#pragma mark - 获取手机中的通讯录以及解析联系人
-(NSMutableArray *)getBook
{
if (personArray == nil) {
personArray = ;
}
//新建一个通讯录类
ABAddressBookRef addressBooks = nil;
if (.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 = 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 = ;
}
}
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;
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中包含所有联系人的信息
;
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 = ;
if (cell == nil){
cell = initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
LBAddressBookVC *book = ;
cell.textLabel.text = book.name;
cell.detailTextLabel.text = book.tel;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
;
LBAddressBookVC *book = ;
NSString * phoneNumber = ;
;
}
#pragma mark - 短信
-(void)sendSmsMessageWithPhoneNumber:(NSString *)phoneNumber
{
if()
{
;
}
else
{
UIAlertView *msgbox = initWithTitle:jekPrompt message:@该设备不支持发短信 delegate:nil cancelButtonTitle:@取消 otherButtonTitles:nil];
;
}
}
-(void)displaySMSComposerSheetPhoneNumber:(NSString *)phoneNumber
{
MFMessageComposeViewController *picker = init];
picker.messageComposeDelegate = (idMFMessageComposeViewControllerDelegate)self;
picker.recipients = ;
picker.body=;
;
}
- (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 = initWithTitle:jekPrompt message:@短信发送失败 delegate:nil cancelButtonTitle:@取消 otherButtonTitles:nil];
;
}
break;
default:
NSLog(@Result: SMS not sent);
break;
}
;
}
@end

版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_958389.html