有人知道是否有可能,以及如何通过官方的sdk/cocoa touch以编程方式从iPhone发送短消息吗?
限制
如果你能在iPhone上的程序中发送短信息,你就可以在后台写一些垃圾信息的游戏。我敢肯定你真的想从你的朋友那里得到垃圾邮件,"试试这个新游戏!它是我的箱子,你的也是!roxersboxxers.com!!!!!如果你现在注册,你将获得3200个RB积分!"
苹果对自动(甚至部分自动)短信和拨号操作有限制。(想象一下如果游戏在一天中的特定时间拨打911)
最好的办法是在Internet上设置一个使用在线短信发送服务的中间服务器,并在需要完全自动化时通过该路由发送短信。(也就是说,你在iPhone上的程序向你的服务器发送一个UDP包,它发送真正的短信)
IOS 4更新
但是,ios 4现在提供了一个viewController,您可以将其导入到应用程序中。您可以预先填充SMS字段,然后用户可以在控制器内启动SMS发送。与使用"sms:…"URL格式不同,这允许您的应用程序保持打开状态,并允许您填充"to"和"body"字段。甚至可以指定多个收件人。
这可以防止应用程序在没有用户明确意识到的情况下发送自动短信。你仍然不能从iPhone本身发送完全自动化的短信,它需要一些用户交互。但这至少允许您填充所有内容,并避免关闭应用程序。
mfMessageComposeviewController类有很好的文档记录,教程显示了实现它的简单性。
IOS 5更新
iOS 5包括iPod touch和iPad设备的消息传递,所以虽然我自己还没有测试过,但可能所有iOS设备都能够通过mfMessageComposeView控制器发送短信。如果是这种情况,那么苹果正在运行一个短信服务器,它代表没有移动调制解调器的设备发送消息。
IOS 6更新
此类没有更改。
IOS 7更新
现在,您可以检查正在使用的邮件媒体是否接受主题或附件,以及它将接受何种类型的附件。您可以编辑主题并在媒体允许的情况下向邮件添加附件。
IOS 8更新
此类没有更改。
IOS 9更新
此类没有更改。
iOS 10更新
此类没有更改。
IOS 11更新
本类无重大变化
此类的限制
请记住,如果没有iOS4,这在手机上是行不通的,在ipod touch或ipad上也行不通,除非是iOS5下的版本。在使用此控制器之前,您必须检测设备和iOS的限制,或者将应用程序限制为最近升级的3G、3GS和4部iPhone。
然而,一个发送短消息的中间服务器将允许任何和所有这些iOS设备发送短消息,只要它们有互联网接入,所以它可能仍然是许多应用程序的一个更好的解决方案。或者,同时使用两者,并且只有在设备不支持时才返回到在线短信服务。
这里有一个教程,它完全按照您的要求来做:MFMessageComposeViewController。
http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/
基本上:
1 2 3 4 5 6 7 8
| MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = @"SMS message here";
controller.recipients = [NSArray arrayWithObjects:@"1(234)567-8910", nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
} |
以及到文档的链接。
https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontroller
必须将messageui.framework添加到Xcode项目中
在头文件中包含一个#import 。
将这些委托添加到头文件MFMessageComposeViewControllerDelegate和UINavigationControllerDelegate中。
在您的IBAction方法中,声明MFMessageComposeViewController的实例,即messageInstance实例。
若要检查设备是否可以在if条件下使用[MFMessageComposeViewController canSendText]发送文本,它将返回yes/no。
在if条件下,执行以下操作:
你的messageInstance的第一组身体是:
1
| messageInstance.body = @"Hello from Shah"; |
然后将邮件的收件人确定为:
1
| messageInstance.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321", nil]; |
将MessageInstance的委托设置为:
1
| messageInstance.messageComposeDelegate = self; |
在最后一行中,请执行以下操作:
1
| [self presentModalViewController:messageInstance animated:YES]; |
您可以使用sms:[target phone number]url打开SMS应用程序,但没有关于如何用文本预先填充SMS正文的指示(请参阅Apple开发者论坛上的此文章)。
XPC是MacOS中进程间通信的系统之一。该系统层是在利用libsystem和launchd传输plist结构的基础上开发的用于进程间通信的。实际上,它是一个接口,允许通过交换字典等结构来管理流程。由于遗传,iOS 5也具有这种机制。
你可能已经明白我所说的引言是什么意思了。是的,iOS中有系统服务,包括用于XPC通信的工具。我想用一个发送短消息的守护进程来举例说明这项工作。不过,应该注意的是,这项功能在iOS 6中是固定的,但与iOS 5.0-5.1.1相关。越狱、私人框架和其他非法工具不需要被利用。只需要目录/usr/include/xpc/*中的头文件集。
iOS中的短信息发送元素之一是系统服务com.apple.chatkit,其任务包括生成、管理和发送短信息。为了便于控制,它具有公共可用的通信端口com.apple.chatkit.clientcomposeserver.xpc。使用xpc子系统,您可以生成和发送消息,而无需用户的批准。
好吧,让我们试着创建一个连接。
1 2 3 4 5
| 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 connection myconnection设置为短消息发送服务。但是,xpc配置提供了挂起连接的创建——我们需要为激活再采取一个步骤。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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
", 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); |
连接已激活。此时,iOS6将在电话日志中显示一条禁止此类通信的消息。现在我们需要生成一个类似于xpc_字典的字典,其中包含消息发送所需的数据。
1 2 3 4 5 6 7 8
| 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端口,并确保它已送达。
1 2 3 4
| xpc_connection_send_message(myConnection, mydict);
xpc_connection_send_barrier(myConnection, ^{
NSLog(@"The message has been successfully delivered");
}); |
这就是全部。短信发送。
添加messageui.framework并使用以下代码
1
| #import <MessageUI/MessageUI.h> |
然后:
1 2 3 4 5 6 7 8
| if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = @"Your Message here";
[messageComposer setBody:message];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
} |
以及委托方法-
1 2 3 4
| - (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
[self dismissViewControllerAnimated:YES completion:nil];
} |
您可以使用以下方法:
1
| [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:MobileNumber"]] |
iOS将自动从您的应用程序导航到消息应用程序的消息撰写页面。由于url的方案以sms:开头,因此它被标识为消息应用程序识别并启动它的类型。
遵循此程序
1.将MessageUI.Framework添加到项目
中。
2。在.h文件中导入#import 。
三。复制此代码以发送消息
1 2 3 4 5 6 7 8
| if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = @"Message!!!";
[messageComposer setBody:message];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
} |
4。如果需要,实现delegate方法。
1 2 3 4 5 6 7
| - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
///your stuff here
[self dismissViewControllerAnimated:YES completion:nil];
} |
快跑!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| //Add the Framework in .h file
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
//Set the delegate methods
UIViewController<UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate>
//add the below code in .m file
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
MFMessageComposeViewController *controller =
[[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
NSString *str= @"Hello";
controller.body = str;
controller.recipients = [NSArray arrayWithObjects:
@"", nil];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:
(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled:
NSLog(@"Cancelled");
break;
case MessageComposeResultFailed:
NSLog(@"Failed");
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
} |
这是iOS中发送短信息的快速代码版本。请注意,它只适用于真正的设备。在iOS 7+中测试的代码。你可以在这里阅读更多。
1)创建继承mfMessageComposeviewControllerDelegate和nsObject的新类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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)如何使用此类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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.")
}
} |
1 2 3 4 5 6 7
| - (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
UIImage *ui =resultimg.image;
pasteboard.image = ui;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]];
} |
//用名称和数字调用方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| -(void)openMessageViewWithName:(NSString*)contactName withPhone:(NSString *)phone{
CTTelephonyNetworkInfo *networkInfo=[[CTTelephonyNetworkInfo alloc]init];
CTCarrier *carrier=networkInfo.subscriberCellularProvider;
NSString *Countrycode = carrier.isoCountryCode;
if ([Countrycode length]>0) //Check If Sim Inserted
{
[self sendSMS:msg recipientList:[NSMutableArray arrayWithObject:phone]];
}
else
{
[AlertHelper showAlert:@"Message" withMessage:@"No sim card inserted"];
} |
}
//发送消息的方法
1 2 3 4 5 6 7 8 9 10 11
| - (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSMutableArray *)recipients{
MFMessageComposeViewController *controller1 = [[MFMessageComposeViewController alloc] init] ;
controller1 = [[MFMessageComposeViewController alloc] init] ;
if([MFMessageComposeViewController canSendText])
{
controller1.body = bodyOfMessage;
controller1.recipients = recipients;
controller1.messageComposeDelegate = self;
[self presentViewController:controller1 animated:YES completion:Nil];
}
} |
iOS4中有一个类,它支持用主体和从应用程序接收的消息发送。它的工作原理与发送邮件相同。您可以在这里找到文档:链接文本
如果需要,可以使用名为CTMessageCenter类的私有框架CoreTelephony。有几种发送短信息的方法。
您可以显示mfmessagecomposeviewcontroller,它可以发送短信息,但有用户提示(他点击发送按钮)。没有用户权限是无法做到这一点的。在iOS11上,您可以进行扩展,就像对传入消息进行过滤一样,告诉iOS它是否是垃圾邮件。不能再使用短信息了
1
| [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:number"]] |
这将是最好和短期的方法。
使用此:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| - (void)showSMSPicker
{
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if (messageClass != nil) {
// Check whether the current device is configured for sending SMS messages
if ([messageClass canSendText]) {
[self displaySMSComposerSheet];
}
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
//feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MessageComposeResultCancelled:
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending canceled!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert1 show];
[alert1 release];
}
// feedbackMsg.text = @"Result: SMS sending canceled";
break;
case MessageComposeResultSent:
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert2 show];
[alert2 release];
}
// feedbackMsg.text = @"Result: SMS sent";
break;
case MessageComposeResultFailed:
{
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS sending failed!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert3 show];
[alert3 release];
}
// feedbackMsg.text = @"Result: SMS sending failed";
break;
default:
{
UIAlertView *alert4 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"SMS not sent!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert4 show];
[alert4 release];
}
// feedbackMsg.text = @"Result: SMS not sent";
break;
}
[self dismissModalViewControllerAnimated: YES];
} |
如果要在自己的应用程序中显示创建和发送消息,则需要使用mfmessagecomposeviewcontroller。
否则,可以使用SharedApplication方法。