[ios] 发短信,从电话薄里选择联系人【转】
1.发短信
引入 MEssageUI.framework
#import "MessageUI/MessageUI.h"
实现这个 <MFMessageComposeViewControllerDelegate>
-(void) sendmyMessage{
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
NSLog(@"can send SMS [%d]", [messageClass canSendText]);
if (messageClass != nil) {
if ([messageClass canSendText]) {
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
controller.body =@"发送内容";
controller.recipients = [NSArray arrayWithObjects:@"1581234567", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
[controller release];
}
}
}
#pragma delegate方法
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
NSString *backMsg;
switch (result) {
case MessageComposeResultSent:
backMsg = @"Success";
break;
case MessageComposeResultCancelled:
backMsg = @"Cancelled";
break;
case MessageComposeResultFailed:
backMsg = @"Failure";
break;
default:
break;
}
NSLog(@"back %@", backMsg);
[self dismissModalViewControllerAnimated:YES];
}
2.选择联系人
AddressBookUI.framework
AddressBook.framework#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>
ABPeoplePickerNavigationControllerDelegate
- (IBAction)showAbbookAdress:(id)sender {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
if(picker){
[self presentModalViewController:picker animated:YES];
}
[picker release];
}
#pragma delegate 方法
ABPeoplePickerNavigationControllerDelegate的方法介绍
(a)peoplePickerNavigationControllerDidCancel:当用户选择取消时调用这个方法,可以在这个方法里取消整个通讯录页面的显示。
(b)peoplePickerNavigationController:shouldContinueAfterSelectingPerson: 当用户选择了通讯录中某一个联系人时调用这个方法,可以在这里获取联系人的信息。如果希望可以继续显示这个联系人更具体的信息,则return YES。否则取消整个通讯录页面的显示并return NO。
(c)peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier: 如果上一个方法返回的是YES,则会显示某一个联系人信息,如果选择了联系人的某一项纪录,就会调用这个方法,可以通过点击选择联系人的某一项信息。如果 希望可以对选择的某一项纪录进行进一步操作,比如直接拨打电话或调用邮箱发送邮件,则return YES。否则取消整个通讯录页面的显示并return NO。
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
//获取联系人姓名
name.text = (NSString*)ABRecordCopyCompositeName(person);
//获取联系人电话
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSMutableArray *phones = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < ABMultiValueGetCount(phoneMulti); i++)
{
NSString *aPhone = [(NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i) autorelease];
NSString *aLabel = [(NSString*)ABMultiValueCopyLabelAtIndex(phoneMulti, i) autorelease];
NSLog(@"PhoneLabel:%@ Phone#:%@",aLabel,aPhone);
if([aLabel isEqualToString:@"_$!<Mobile>!$_"])
{
[phones addObject:aPhone];
}
}
phoneNo.text=@"";
if([phones count]>0)
{
NSString *mobileNo = [phones objectAtIndex:0];
phoneNo.text = mobileNo;
//NSLog(mobileNo);
}
//获取联系人邮箱
ABMutableMultiValueRef emailMulti = ABRecordCopyValue(person, kABPersonEmailProperty);
NSMutableArray *emails = [[NSMutableArray alloc] init];
for (i = 0;i < ABMultiValueGetCount(emailMulti); i++)
{
NSString *emailAdress = [(NSString*)ABMultiValueCopyValueAtIndex(emailMulti, i) autorelease];
[emails addObject:emailAdress];
}
email.text=@"";
if([emails count]>0)
{
NSString *emailFirst=[emails objectAtIndex:0];
email.text = emailFirst;
//NSLog(emailFirst);
}
[peoplePicker dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
以上需要真机调试