1.进入通讯录页面,当然要记住dismiss
peoPicker = [[ABPeoplePickerNavigationController alloc] init];
peoPicker.peoplePickerDelegate = self;
[self presentViewController:peoPicker animated:YES completion:nil];
2.设置代理方法 dismiss
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[peoPicker dismissModalViewControllerAnimated:YES];
}
3.当点击联系人 当这个返回值为yes时 可以进入用户详情页面 为no时则不进行下一步操作
// Called after a value has been selected by the user.
// Return YES if you want default action to be performed.
// Return NO to do nothing (the delegate is responsible for dismissing the peoplePicker).
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
NSString *phoneContact;
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
if ((phone != nil)&&ABMultiValueGetCount(phone)>0) {
for (int m = 0; m < ABMultiValueGetCount(phone); m++) {
NSString * aPhone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, m) ;
NSString * aLabel = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(phone, m) ;
if ([aLabel isEqualToString:@"_$!<Mobile>!$_"]) {
phoneContact = aPhone;
}
if ([aLabel isEqualToString:@"_$!<WorkFAX>!$_"]) {
phoneContact = aPhone;
}
if ([aLabel isEqualToString:@"_$!<Work>!$_"]) {
phoneContact = aPhone;
}
if ([aLabel isEqualToString:@"_$!<Home>!$_"]) {
phoneContact = aPhone;
}
}
}
if (phoneContact&&phoneContact.length>0) {
[self sendMessage:phoneContact];
}else{
[self showAlertStr:@"对不起,该联系人无可用电话号码"];
}
return NO;
}
4.发送短信
- (void)sendMessage:(NSString *)phoneNum{
if( [MFMessageComposeViewController canSendText] ){
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init]; //autorelease];
controller.recipients = [NSArray arrayWithObject:phoneNum];
controller.body = @"老友,以“面具会议、讲后即焚”为特色的职场社交【贵人】真不错,您也来吧!贵人下载地址:http://guirenhui.cn";
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
[[[[controller viewControllers] lastObject] navigationItem] setTitle:@"测试短信"];//修改短信界面标题
}else{
[self showAlertStr:@"设备没有短信功能"];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
[controller dismissModalViewControllerAnimated:NO];//关键的一句 不能为YES
switch ( result ) {
case MessageComposeResultCancelled:
[self showAlertStr:@"发送取消"];
break;
case MessageComposeResultFailed:// send failed
[self showAlertStr:@"发送成功"];
break;
case MessageComposeResultSent:
[self showAlertStr:@"发送失败"];
break;
default:
break;
}
}