iOS开发之调用系统发短信接口发送指定短信
2016-08-05 17:22
721 查看
参考来源:iOS开发之调用系统打电话发短信接口以及程序内发短信:
http://www.cnblogs.com/ludashi/p/4037475.html
ios调用短信接口发送指定内容的消息
http://blog.csdn.net/kiki1985/article/details/8772306
获取通讯录
导入头文件:#import <AddressBook/AddressBook.h>
- (void)getAddressBook{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error){
CFErrorRef *error1 = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error1);
[self copyAddressBook:addressBook];
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
[self copyAddressBook:addressBook];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
// 更新界面
//[hud turnToError:@"没有获取通讯录权限"];
});
}
}
把通讯录拷贝到项目中
- (void)copyAddressBookToAppWithAddressBook:(ABAddressBookRef)addressBook{
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
for ( int i = 0; i < numberOfPeople; i++){
ATBAddressBook *addressBook = [[ATBAddressBook alloc] init];
ABRecordRef person = CFArrayGetValueAtIndex(people, i);
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
addressBook.personName = [NSString stringWithFormat:@"%@%@",lastName,firstName];
//读取电话多值
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int k = 0; k<ABMultiValueGetCount(phone); k++)
{
//获取电话Label
NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
//获取該Label下的电话值
NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
addressBook.personPhone = personPhone;
addressBook.phoneType = personPhoneLabel;
}
[_addressArr addObject:addressBook];
}
}
在实现发送短信的类中导入头文件 #import <MessageUI/MessageUI.h>
并遵守两个协议<MFMessageComposeViewControllerDelegate,UINavigationControllerDelegate>
在发送信息按钮的响应事件中执行下面的代码:
-(void)sendMessage:(NSString *)message withPhoneNum:(NSString *)phoneNum
{
//实例化MFMessageComposeViewController,并设置委托
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.delegate = self;
// //拼接并设置短信内容
// NSString *messageContent = [NSString stringWithFormat:@"发送短信测试"];
messageController.body = message;
//设置发送给谁
messageController.recipients = @[phoneNum];
//推到发送试图控制器
[self presentViewController:messageController animated:YES completion:^{
}];
}
实现协议中的方法
//发送短信后回调的方法
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
NSString *tipContent;
switch (result) {
case MessageComposeResultCancelled:
tipContent = @"发送短信已取消";
break;
case MessageComposeResultFailed:
tipContent = @"发送短信失败";
break;
case MessageComposeResultSent:
tipContent = @"发送成功";
break;
default:
break;
}
UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:tipContent delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[alterView show];
}
这样指定的短信就可以发送出去了。(创建tableview保存通讯录信息,在vc中保存到通讯录信息)
http://www.cnblogs.com/ludashi/p/4037475.html
ios调用短信接口发送指定内容的消息
http://blog.csdn.net/kiki1985/article/details/8772306
获取通讯录
导入头文件:#import <AddressBook/AddressBook.h>
- (void)getAddressBook{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error){
CFErrorRef *error1 = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error1);
[self copyAddressBook:addressBook];
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
[self copyAddressBook:addressBook];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
// 更新界面
//[hud turnToError:@"没有获取通讯录权限"];
});
}
}
把通讯录拷贝到项目中
- (void)copyAddressBookToAppWithAddressBook:(ABAddressBookRef)addressBook{
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
for ( int i = 0; i < numberOfPeople; i++){
ATBAddressBook *addressBook = [[ATBAddressBook alloc] init];
ABRecordRef person = CFArrayGetValueAtIndex(people, i);
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
addressBook.personName = [NSString stringWithFormat:@"%@%@",lastName,firstName];
//读取电话多值
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int k = 0; k<ABMultiValueGetCount(phone); k++)
{
//获取电话Label
NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
//获取該Label下的电话值
NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
addressBook.personPhone = personPhone;
addressBook.phoneType = personPhoneLabel;
}
[_addressArr addObject:addressBook];
}
}
在实现发送短信的类中导入头文件 #import <MessageUI/MessageUI.h>
并遵守两个协议<MFMessageComposeViewControllerDelegate,UINavigationControllerDelegate>
在发送信息按钮的响应事件中执行下面的代码:
-(void)sendMessage:(NSString *)message withPhoneNum:(NSString *)phoneNum
{
//实例化MFMessageComposeViewController,并设置委托
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.delegate = self;
// //拼接并设置短信内容
// NSString *messageContent = [NSString stringWithFormat:@"发送短信测试"];
messageController.body = message;
//设置发送给谁
messageController.recipients = @[phoneNum];
//推到发送试图控制器
[self presentViewController:messageController animated:YES completion:^{
}];
}
实现协议中的方法
//发送短信后回调的方法
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
NSString *tipContent;
switch (result) {
case MessageComposeResultCancelled:
tipContent = @"发送短信已取消";
break;
case MessageComposeResultFailed:
tipContent = @"发送短信失败";
break;
case MessageComposeResultSent:
tipContent = @"发送成功";
break;
default:
break;
}
UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:tipContent delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[alterView show];
}
这样指定的短信就可以发送出去了。(创建tableview保存通讯录信息,在vc中保存到通讯录信息)
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- About SVN
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- CentOS 6.5搭建Apache整合SVN 1.8.5服务器(多版本库权限配置)
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- CentOS下SVN服务器测试版安装记录
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- 如何在本机搭建SVN服务器
- Windows下搭建本地SVN服务器
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 基于JavaScript短信验证码如何实现
- Android接收和发送短信处理
- Android开发中实现发送短信的小程序示例
- 基于JavaScript实现手机短信按钮倒计时(超简单)
- 让GoogleCode的SVN下的HTML文件在FireFox下正常显示.
- Windows下SVN服务器搭建方法整理(apache)
- Apache2+SVN+MYSQL认证 配置项详细步骤
- Android短信操作常见协议和常用代码