今天工作遇到要发短信(ios)的功能,于是随手记录了一下
ios中发送短信有两种
1.程序外调用系统短信
2.程序内调用系统发短信
第一种比较简单,直接调用url就可以了
oc下的代码为
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"sms:%@",@""]]]
oc版
swift下的代码
UIApplication.sharedApplication().openURL(NSURL(string: "sms:13888888888")!)
第二种
oc下的代码
1)导入MessageUI.framework,并引入头文件:
#import
2)实现代理方法MFMessageComposeViewControllerDelegate
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:nil];
switch (result) {
case MessageComposeResultSent:
//信息传送成功 break;
case MessageComposeResultFailed:
//信息传送失败 break;
case MessageComposeResultCancelled:
//信息被用户取消传送 break;
default:
break;
}
}
3)发送短信
-(void)showMessageView:(NSArray *)phones title:(NSString *)title body:(NSString *)body
{
if( [MFMessageComposeViewController canSendText] )
{
MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
controller.recipients = phones;
controller.navigationBar.tintColor = [UIColor redColor];
controller.body = body;
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
[[[[controller viewControllers] lastObject] navigationItem] setTitle:title];//修改短信界面标题
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@提示信息
message:@该设备不支持短信功能
delegate:nil
cancelButtonTitle:@确定
otherButtonTitles:nil, nil];
[alert show];
}
}
参数phones:发短信的手机号码的数组,数组中是一个即单发,多个即群发。
4)调用发短信的方法
[self showMessageView:[NSArray arrayWithObjects:@,@, nil] title:@test body:@你是土豪么,么么哒];
Swift版本
import MessageUI
之后让vc继承MFMessageCompose的代理:
class CaipinDetailViewController: UIViewController,MFMessageComposeViewControllerDelegate
func canSendText() -> Bool{
return MFMessageComposeViewController.canSendText()
}//用来指示一条消息能否从用户处发送
func configuredMessageComposeViewController() -> MFMessageComposeViewController{
let messageComposeVC = MFMessageComposeViewController()
messageComposeVC.messageComposeDelegate = self
messageComposeVC.body = "HI! \(caipinArray[0].rest) 的 \(caipinArray[0].name) 味道很不错,邀你共享 -来自SoFun的邀请"
return messageComposeVC }
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
然后在按钮的action方法中加入以下代码:
@IBAction func share(sender: UIButton) {
let shareView = ShareViewController()
self.presentViewController(shareView, animated: true, completion: nil)
}
@IBAction func message(sender: UIButton) {
if self.canSendText(){
let messageVC = self.configuredMessageComposeViewController()
presentViewController(messageVC, animated: true, completion: nil)
} else {
let errorAlert = UIAlertView(title: "不能发送", message: "你的设备没有短信功能", delegate: self, cancelButtonTitle: "取消")
}
}
我们在真机上测试一下,效果图:

今天工作遇到要发短信(ios)的功能,于是随手记录了一下的更多相关文章
- ios 调用系统发短信以及打电话功能
先介绍一种最简单的方法: 调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://100 ...
- eclipse开发安卓 发短信打电话发送邮件功能
1.在mainfiest中添加 //添加拨打电话的功能 <uses-permission android:name="android.permission.CALL_PHON ...
- iOS - 打电话, 发短信
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- iOS学习笔记(十四)——打电话、发短信
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- iOS基本的发短信和打电话调用
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- ios打电话发短信接口
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- IOS,发短信,发邮件,打电话
今天把APP里常用小功能 例如发短信.发邮件.打电话.全部拿出来简单说说它们的实现思路. 1.发短信实现打电话的功能,主要二种方法,下面我就分别说说它们的优缺点.1.1.发短信(1)——URL // ...
- iOS打电话、发短信、发邮件功能开发
本文转载至 http://www.lvtao.net/ios/506.html 今天把APP里常用小功能 例如发短信.发邮件.打电话.全部拿出来简单说说它们的实现思路. 1.发短信实现打电话的功能,主 ...
- iOS开发之调用系统打电话发短信接口以及程序内发短信
在本篇博客开头呢,先说一下写本篇的博客的原因吧.目前在做一个小项目,要用到在本应用程序内发验证码给其他用户,怎么在应用内发送短信的具体细节想不大起来了,于是就百度了一下,发现也有关于这方面的博客,点进 ...
随机推荐
- 快速学习bootstrap前台框架
W3c里的解释 使用bootstrap需要注意事项 1. 在html文件第一行要加上<!doctype html>[s1] 2. 导入bootstrap.min.css文件 3. 导 ...
- Unix编程之size_t、ssize_t
http://blog.csdn.net/lalor/article/details/7426184 首先,我非常肯定以 及确定的告诉你ssize_t是有符号整型,在32位机器上等同与int,在64位 ...
- 安装nginx创建错误
./configure: error: the HTTP gzip module requires the zlib library. 解决: yum install -y zlib-devel -- ...
- NC / Netcat - 反弹Shell
原理 实验环境: 攻击机:windows机器,IP:192.168.12.109 受害机:linux机器,IP:192.168.79.1 攻击机:设置本地监听端口2222 C:\netcat>n ...
- qtp与selenium2的区别
QTP: 我觉得qtp适合的人: 对编程不是很熟悉的 厌烦了手动的功能测试,想快速进入自动化行业的 公司想快速自动化项目,并且对价格或者对盗版无所谓的 vbs脚本语言易于上手,可以培训你对代码的兴趣 ...
- ActiveMQ 集群(1)
Queue consumer clusters(消费者集群): 简介: 同一个queue,如果一个消费者失效, 那么任何未经确认的消息将会被发送给queue上的其它消费者.如果一个消费者比其它消费者执 ...
- C# 对象与JSON串互相转换
using System;using System.IO;using System.Text;using Newtonsoft.Json; namespace OfflineAcceptControl ...
- [HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 RT,该题要求每次更新是更新所有节点,分别求平方根,查询是求和.昨晚思前想后找有没有一个数学上的 ...
- C++ string类的学习
string类对于处理字符串的一些应用非常的方便,我个人感觉,string和字符数组const char *很像,而且又比字符数组用起来方便的多. 注意其删除,取子串,插入等函数里面都有一个重载版本是 ...
- CentOS 6.5系统使用yum方式安装LAMP环境和phpMyAdmin详细过程
介绍如何在CentOs6.2下面使用YUM配置安装LAMP环境,一些兄弟也很喜欢使用编译的安装方法,个人觉得如果不是对服务器做定制,用yum安装稳定简单,何必去download&make&am ...