import Foundation
import MessageUI
class MessageComposer: NSObject, MFMessageComposeViewControllerDelegate {
// A wrapper function to indicate whether or not a text message can be sent from the user's device
func canSendText() -> Bool {
return MFMessageComposeViewController.canSendText()
}
// Configures and returns a MFMessageComposeViewController instance
func configuredMessageComposeViewController(textMessageRecipients:[String] ,textBody body:String) -> MFMessageComposeViewController {
let messageComposeVC = MFMessageComposeViewController()
messageComposeVC.messageComposeDelegate = self // Make sure to set this property to self, so that the controller can be dismissed!
messageComposeVC.recipients = textMessageRecipients
messageComposeVC.body = body
return messageComposeVC
}
// MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}
2)如何使用这个类:
func openMessageComposerHelper(sender:AnyObject ,withIndexPath indexPath: NSIndexPath) {
var recipients = [String]()
//modify your recipients here
if (messageComposer.canSendText()) {
println("can send text")
// Obtain a configured MFMessageComposeViewController
let body = Utility.createInvitationMessageText()
let messageComposeVC = messageComposer.configuredMessageComposeViewController(recipients, textBody: body)
// Present the configured MFMessageComposeViewController instance
// Note that the dismissal of the VC will be handled by the messageComposer instance,
// since it implements the appropriate delegate call-back
presentViewController(messageComposeVC, animated: true, completion: nil)
} else {
// Let the user know if his/her device isn't able to send text messages
self.displayAlerViewWithTitle("Cannot Send Text Message", andMessage: "Your device is not able to send text messages.")
}
}
12
iOS 4中有一个类,它支持从您的应用程序发送带有正文和配方的消息 . 它与发送邮件的工作方式相同 . 你可以在这里找到文件:link text
18 回答
限制
如果您可以在iPhone上的程序中发送短信,您将能够编写在后台垃圾邮件的游戏 . 我确定你真的想要从你的朋友那里得到垃圾邮件,“试试这个新游戏吧!它会让我的箱子变得干净,而你的箱子也会出现!roxxersboxxers.com !!!!如果你现在注册,你将获得3,200 RB点!”
Apple对自动(甚至部分自动化)SMS和拨号操作有限制 . (想象一下,如果游戏在一天的特定时间拨打911)
您最好的办法是在互联网上设置一个使用在线短信发送服务的中间服务器,如果您需要完全自动化,则通过该路由发送短信 . (即,你的iPhone上的程序向你的服务器发送一个UDP数据包,发送真正的短信)
iOS 4更新
但是,iOS 4现在提供了一个可以导入到应用程序中的视图控制器 . 您预填充SMS字段,然后用户可以在控制器内启动SMS发送 . 与使用"sms:..." url格式不同,这允许您的应用程序保持打开状态,并允许您填充to和body字段 . 您甚至可以指定多个收件人 .
这可以防止应用程序在用户没有明确意识到的情况下发送自动SMS . 您仍然无法从iPhone本身发送完全自动化的短信,它需要一些用户互动 . 但这至少允许您填充所有内容,并避免关闭应用程序 .
MFMessageComposeViewController类已有详细记录,tutorials显示了它的实施容易程度 .
iOS 5更新
iOS 5包括iPod touch和iPad设备的消息,因此虽然我自己还没有测试过,但可能是所有iOS设备都可以通过MFMessageComposeViewController发送短信 . 如果是这种情况,那么Apple正在运行一个SMS服务器,它代表没有蜂窝调制解调器的设备发送消息 .
iOS 6更新
没有改变这个类 .
iOS 7更新
您现在可以检查您正在使用的消息媒体是否接受主题或附件,以及它将接受哪种附件 . 您可以编辑主题并在消息中添加附件,介质允许它 .
iOS 8更新
没有改变这个类 .
iOS 9更新
没有改变这个类 .
iOS 10更新
没有改变这个类 .
iOS 11更新
No significant changes to this class
此课程的限制
请记住,这不适用于没有iOS 4的手机,除了iOS 5之外,它无法在iPod touch或iPad上运行 . 在使用此功能之前,您必须检测设备和iOS限制控制器,或将您的应用程序限制在最近升级的3G,3GS和4个iPhone的风险 .
但是,发送SMS的中间服务器将允许任何和所有这些iOS设备发送SMS,只要它们具有Internet访问权限,因此对于许多应用程序来说它仍然是更好的解决方案 . 或者,使用两者,并且只有在设备不支持时才会回退到在线SMS服务 .
这是一个完全符合您要求的教程:
MFMessageComposeViewController
.http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/
实质上:
以及文档的链接 .
https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontroller
您必须将MessageUI.framework添加到Xcode项目中
在头文件中包含
#import <MessageUI/MessageUI.h>
将这些代理添加到头文件
MFMessageComposeViewControllerDelegate
&UINavigationControllerDelegate
在您的
IBAction
方法声明MFMessageComposeViewController
的实例中说messageInstance
要检查您的设备是否可以在if条件下发送文本使用
[MFMessageComposeViewController canSendText]
,它将返回是/否在
if
条件中执行以下操作:首先为
messageInstance
设置正文:您可以使用
sms:[target phone number]
URL打开SMS应用程序,但没有关于如何使用文本预填充SMS正文的指示(请参阅Apple Developer论坛上的this post) .MacOS中的进程间通信系统之一是XPC . 该系统层是基于使用libSystem和launchd传输plist结构而开发的进程间通信 . 实际上,它是一个允许通过交换诸如字典之类的结构来管理进程的接口 . 由于遗传,iOS 5也拥有这种机制 .
您可能已经理解了这个介绍的含义 . 是的,iOS中有系统服务,包括用于XPC通信的工具 . 我想用一个用于短信发送的守护进程来举例说明这项工作 . 但是,应该提到的是,这种能力在iOS 6中已得到修复,但与iOS 5.0-5.1.1相关 . 它不需要越狱,私人框架和其他非法工具开发 . 只需要来自目录/ usr / include / xpc / *的头文件集 .
在iOS中发送SMS的元素之一是系统服务com.apple.chatkit,其任务包括生成,管理和发送短文本消息 . 为了便于控制,它具有公共可用的通信端口com.apple.chatkit.clientcomposeserver.xpc . 使用XPC子系统,您可以在未经用户批准的情况下生成和发送消息 .
好吧,让我们尝试创建一个连接 .
现在我们将XPC连接myConnection设置为SMS发送服务 . 但是,XPC配置提供了挂起连接的创建 - 我们需要再激活一步 .
连接已激活 . 此时iOS 6将在电话日志中显示禁止此类通信的消息 . 现在我们需要生成一个类似于xpc_dictionary的字典,其中包含发送消息所需的数据 .
剩下的就是:将消息发送到XPC端口并确保它已交付 .
就这样 . 发送短信 .
添加MessageUI.Framework并使用以下代码
然后:
和委托方法 -
您可以使用此方法:
iOS将自动从您的应用导航到消息app 's message composing page. Since the URL' s方案以sms:开头,这被识别为消息应用程序识别并启动它的类型 .
Follow this procedures
1 . 添加
MessageUI.Framework
到项目2 . 在.h文件中导入
#import <MessageUI/MessageUI.h>
.3 . 复制此代码以发送消息
4 . 如果您愿意,可以实施
delegate
方法 .跑吧!
这是在iOS中发送SMS的Swift版代码 . 请注意,它仅适用于真实设备 . 在iOS 7中进行了代码测试 . 你可以阅读更多here .
1)创建一个继承MFMessageComposeViewControllerDelegate和NSObject的新类:
2)如何使用这个类:
iOS 4中有一个类,它支持从您的应用程序发送带有正文和配方的消息 . 它与发送邮件的工作方式相同 . 你可以在这里找到文件:link text
//使用名称和号码调用方法 .
}
//发送消息的方法
如果需要,可以使用名为
CTMessageCenter
class的私有框架CoreTelephony
. 发送短信有几种方法 .用这个:
这将是最好和最短的方式 .
你可以提供MFMessageComposeViewController,它可以发送短信,但有用户提示(他点击发送按钮) . 没有用户许可,无法做到这一点 . 在iOS 11上,您可以进行扩展,这可以像过滤消息一样,告诉iOS其垃圾邮件与否 . 没有更多的短信无法完成
如果要在自己的应用程序中显示创建和发送消息,则需要使用 MFMessageComposeViewController .
否则,您可以使用 sharedApplication 方法 .