有没有人知道,通过官方SDK / Cocoa Touch,从iPhone发送短信是否可行,以及如何实现?
当前回答
如果您愿意,您可以使用名为CTMessageCenter类的私有框架CoreTelephony。有一些发送短信的方法。
其他回答
You must add the MessageUI.framework to your Xcode project Include an #import <MessageUI/MessageUI.h> in your header file Add these delegates to your header file MFMessageComposeViewControllerDelegate & UINavigationControllerDelegate In your IBAction method declare instance of MFMessageComposeViewController say messageInstance To check whether your device can send text use [MFMessageComposeViewController canSendText] in an if condition, it'll return Yes/No In the if condition do these: First set body for your messageInstance as: messageInstance.body = @"Hello from Shah"; Then decide the recipients for the message as: messageInstance.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil]; Set a delegate to your messageInstance as: messageInstance.messageComposeDelegate = self; In the last line do this: [self presentModalViewController:messageInstance animated:YES];
您可以使用sms:[目标电话号码]URL打开sms应用程序,但没有指示如何用文本预填充sms正文。
下面是在iOS中发送短信的Swift版本代码。请注意,它只在实际设备中工作。在iOS 7+中测试的代码。你可以在这里阅读更多。
1)创建一个继承MFMessageComposeViewControllerDelegate和NSObject的新类:
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.")
}
}
限制
如果你可以在iPhone的程序中发送短信,你就可以在后台编写垃圾邮件游戏。我相信你一定很想收到朋友的垃圾邮件,“试试这款新游戏!它撕破了我的短裤,你的也会!roxxersboxxers.com ! !如果你现在注册,你将得到3200个RB点!!”
苹果对自动(甚至部分自动)短信和拨号操作有限制。(想象一下如果游戏在一天的特定时间拨打911)
最好的办法是在互联网上设置一个使用在线SMS发送服务的中间服务器,如果需要完全自动化,则通过该路由发送SMS。(例如,iPhone上的程序向服务器发送UDP数据包,服务器发送真正的短信)
iOS 4更新
然而,iOS 4现在提供了一个viewController,你可以导入到你的应用程序中。预先填充SMS字段,然后用户可以在控制器内发起SMS发送。与使用“SMS:…”url格式不同,这允许您的应用程序保持打开状态,并允许您填充to和body字段。您甚至可以指定多个收件人。
这可以防止应用程序在用户没有明确意识到的情况下发送自动SMS。你仍然不能从iPhone本身发送完全自动化的短信,它需要一些用户交互。但这至少允许您填充所有内容,并避免关闭应用程序。
MFMessageComposeViewController类有很好的文档,并且教程展示了它的实现是多么容易。
iOS 5更新
iOS 5包括iPod touch和iPad设备的消息,所以虽然我还没有亲自测试,但可能所有iOS设备都可以通过MFMessageComposeViewController发送短信。如果是这样的话,那么苹果正在运行一个SMS服务器,它代表没有蜂窝调制解调器的设备发送消息。
iOS 6更新
这个类没有变化。
iOS 7更新
您现在可以检查您正在使用的消息媒体是否接受主题或附件,以及它将接受哪种附件。您可以编辑主题并在媒体允许的情况下向邮件添加附件。
iOS 8更新
这个类没有变化。
iOS 9更新
这个类没有变化。
iOS 10更新
这个类没有变化。
iOS 11更新
这个类没有显著的变化
该类的限制
请记住,这在没有iOS 4的手机上是行不通的,在iPod touch或iPad上也行不通,除非是在iOS 5下。在使用这个控制器之前,你必须检测设备和iOS的限制,否则你的应用可能会被限制在最近升级的3G、3GS和4部iphone上。
然而,发送短信的中间服务器将允许任何和所有这些iOS设备发送短信,只要他们有互联网接入,所以它可能仍然是许多应用程序的一个更好的解决方案。或者,两者都使用,只有在设备不支持在线短信服务时才使用在线短信服务。
MacOS中进程间通信的系统之一是XPC。该系统层是基于libSystem和launchd的plist结构传输而开发的进程间通信层。事实上,它是一个允许通过交换字典等结构来管理进程的接口。由于遗传,iOS 5也拥有这种机制。
你可能已经明白我的介绍是什么意思。是的,iOS中的系统服务包括用于XPC通信的工具。我想用一个用于SMS发送的守护进程来举例说明工作。但是,应该提到的是,这个能力在iOS 6中是固定的,但与iOS 5.0-5.1.1相关。越狱、私有框架和其他非法工具都不需要使用它。只需要目录/usr/include/xp /*中的头文件集。
iOS中用于发送SMS的元素之一是系统服务com.apple。Chatkit,其任务包括生成、管理和发送短文本消息。为了便于控制,它具有公开可用的通信端口com.apple.chatkit.clientcomposeserver.xpc。使用XPC子系统,您可以在不需要用户批准的情况下生成和发送消息。
好吧,让我们试着建立联系。
xpc_connection_t myConnection;
dispatch_queue_t queue = dispatch_queue_create("com.apple.chatkit.clientcomposeserver.xpc", DISPATCH_QUEUE_CONCURRENT);
myConnection = xpc_connection_create_mach_service("com.apple.chatkit.clientcomposeserver.xpc", queue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
现在我们已经将XPC连接myConnection设置为SMS发送服务。但是,XPC配置提供了创建挂起连接的功能——我们需要再采取一步来激活。
xpc_connection_set_event_handler(myConnection, ^(xpc_object_t event){
xpc_type_t xtype = xpc_get_type(event);
if(XPC_TYPE_ERROR == xtype)
{
NSLog(@"XPC sandbox connection error: %s\n", xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
}
// Always set an event handler. More on this later.
NSLog(@"Received a message event!");
});
xpc_connection_resume(myConnection);
连接已激活。此时此刻,iOS 6将在电话日志中显示一条消息,禁止这种类型的通信。现在,我们需要生成一个类似于xpc_dictionary的字典,其中包含消息发送所需的数据。
NSArray *recipient = [NSArray arrayWithObjects:@"+7 (90*) 000-00-00", nil];
NSData *ser_rec = [NSPropertyListSerialization dataWithPropertyList:recipient format:200 options:0 error:NULL];
xpc_object_t mydict = xpc_dictionary_create(0, 0, 0);
xpc_dictionary_set_int64(mydict, "message-type", 0);
xpc_dictionary_set_data(mydict, "recipients", [ser_rec bytes], [ser_rec length]);
xpc_dictionary_set_string(mydict, "text", "hello from your application!");
剩下的不多了:将消息发送到XPC端口,并确保它被交付。
xpc_connection_send_message(myConnection, mydict);
xpc_connection_send_barrier(myConnection, ^{
NSLog(@"The message has been successfully delivered");
});
这是所有。短信发送。
推荐文章
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- Swift编译器错误:“框架模块内的非模块化头”
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容