【问题标题】:How to programmatically send SMS on the iPhone?如何以编程方式在 iPhone 上发送短信?
【发布时间】:2010-09-05 20:48:53
【问题描述】:

有人知道是否有可能以及如何使用官方 SDK / Cocoa Touch 以编程方式从iPhone 发送SMS

【问题讨论】:

  • 在 iOS 4 中您可以发送带有代码的短信,但问题是您的应用程序将被关闭
  • 如果其他人不需要使用官方 SDK,我写了这篇文章来展示如何使用 Twilio:twilio.com/blog/2016/11/…
  • 您可以从您的 gmail 中创建一个 google 电子表格,创建您想要的字段,然后执行 tools->script editor 并在 POST 请求中使用 MailApp.sendEmail api 将电子邮件发送到电话号码。 att 是 YOURNUMBER@mms.att.net,tmobile 是 YOURNUMBER@tmomail.net 我认为(全部免费)

标签: ios objective-c cocoa-touch sms


【解决方案1】:

限制

如果您可以在 iPhone 上的程序中发送 SMS,您将能够编写在后台向人们发送垃圾邮件的游戏。我敢肯定你真的很想收到朋友的垃圾邮件,“试试这个新游戏!它让我的 boxxers 感到兴奋,你的也会如此!roxxersboxxers.com!!!!如果你现在注册,你将获得 3,200 RB积分!!”

Apple 对自动化(甚至部分自动化)短信和拨号操作有限制。 (想象一下,如果游戏在一天中的特定时间拨打 911)

您最好的选择是在互联网上设置一个使用在线 SMS 发送服务的中间服务器,如果您需要完全自动化,则通过该路由发送 SMS。 (即你在 iPhone 上的程序向你的服务器发送一个 UDP 数据包,由它发送真正的 SMS)

iOS 4 更新

然而,iOS 4 现在提供了一个viewController,您可以将其导入到您的应用程序中。您预填充 SMS 字段,然后用户可以在控制器内启动 SMS 发送。与使用“SMS:...” url 格式不同,这允许您的应用程序保持打开状态,并允许您填充 tobody 字段。您甚至可以指定多个收件人。

这可以防止应用程序在用户未明确意识到的情况下发送自动 SMS。您仍然无法从 iPhone 本身发送全自动短信,它需要一些用户交互。但这至少允许您填充所有内容,并避免关闭应用程序。

MFMessageComposeViewController 类有据可查,tutorials 显示了它的实现是多么容易。

iOS 5 更新

iOS 5 包括用于 iPod touch 和 iPad 设备的消息传递,因此虽然我自己还没有对此进行测试,但可能所有 iOS 设备都能够通过 MFMessageComposeViewController 发送 SMS。如果是这种情况,那么 Apple 正在运行一个 SMS 服务器,它代表没有蜂窝调制解调器的设备发送消息。

iOS 6 更新

这个类没有变化。

iOS 7 更新

您现在可以检查您使用的消息媒体是否接受主题或附件,以及它接受的附件类型。您可以在媒体允许的情况下编辑主题并向邮件添加附件。

iOS 8 更新

这个类没有变化。

iOS 9 更新

这个类没有变化。

iOS 10 更新

这个类没有变化。

iOS 11 更新

No significant changes to this class

对此类的限制

请记住,这不适用于没有 iOS 4 的手机,也不能在 iPod touch 或 iPad 上运行,可能在 iOS 5 下除外。您必须事先检测设备和 iOS 限制使用此控制器,或冒险将您的应用程序限制为最近升级的 3G、3GS 和 4 部 iPhone。

但是,发送 SMS 的中间服务器将允许任何和所有这些 iOS 设备发送 SMS,只要它们可以访问 Internet,因此对于许多应用程序来说,它可能仍然是一个更好的解决方案。或者,两者都使用,并且仅在设备不支持时才回退到在线 SMS 服务。

【讨论】:

  • 如果您购买了该域名,我将再也无法以同样的方式看待您。
  • 我认为有人将这篇文章标记为垃圾邮件很讽刺。阅读字里行间,人们!
  • -1 因为这读作推测。另外,请参阅 Jus 的 Wondrin 和 rydgaze 对如何发送应用内短信的回答。
  • @Frank - 更新了我的文章以反映 iOS 4 的新功能。删除了空洞的措辞。
  • 非常感谢您为新的 iOS 版本更新这篇文章。 :)
【解决方案2】:

这是一个完全符合您要求的教程:MFMessageComposeViewController

http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/

基本上:

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

【讨论】:

  • 仍然必须弹出 SMS 表单。有什么方法可以在后台发送?
  • 您当然可以使用 Twilio 等服务在后台发送短信,但如果您想从用户的电话号码发送,则必须通过上述方法批准该消息。
  • 任何使用上述代码的人都可能希望考虑将MFMessageComposeViewController *controller = ... 放在 if 块中。 (类方法不需要有实例来做测试)
  • 链接http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/ 在我的笔记本电脑上显示“502 Bad Gateway”。可能链接坏了。
【解决方案3】:
  1. 您必须将 MessageUI.framework 添加到您的 Xcode 项目中
  2. 在头文件中包含#import <MessageUI/MessageUI.h>
  3. 将这些委托添加到您的头文件MFMessageComposeViewControllerDelegate & UINavigationControllerDelegate
  4. 在您的IBAction 方法中声明MFMessageComposeViewController 的实例,例如messageInstance
  5. 要检查您的设备是否可以发送文本,请在 if 条件下使用 [MFMessageComposeViewController canSendText],它将返回 Yes/No
  6. if 条件下执行以下操作:

    1. messageInstance 的第一组正文为:

      messageInstance.body = @"Hello from Shah";
      
    2. 然后将邮件的收件人确定为:

      messageInstance.recipients = [NSArray arrayWithObjects:@"12345678", @"87654321",         nil];
      
    3. 将您的 messageInstance 的委托设置为:

      messageInstance.messageComposeDelegate = self;
      
    4. 在最后一行这样做:

      [self presentModalViewController:messageInstance animated:YES];
      

【讨论】:

  • 版本说明:presentModalViewController:animated: 已弃用;请改用presentViewController:animated:completion:。另外,如果你想知道结果,记得定义委托方法- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
  • 请放上代码进行测试。 @Najeebullah Shah 在这里或在 github 中。
【解决方案4】:

您可以使用sms:[target phone number] URL 打开 SMS 应用程序,但没有说明如何使用文本预填充 SMS 正文。

【讨论】:

  • react-native-communications 在打开 sms:targetphonenumber&body=textyouwanttoprefillwith for iOS 时使用 &body= 参数预填充文本
  • 我刚刚在我的 iphone 8 上试过这个,效果很好......谢谢!
  • 我需要向特定号码发送短信,但需要预定义(预填充)正文。正文应该是从 4 个可能选项中手动选择的。例如。应用程序打开并弹出“选择原因”。然后有 4 个选项可用。接下来,用户选择这 4 个选项之一,然后发送自动 SMS,其中包含用户的选择作为正文。有什么建议吗?
【解决方案5】:

MacOS 中的进程间通信系统之一是 XPC。该系统层已开发用于基于使用 libSystem 和 launchd 传输 plist 结构的进程间通信。事实上,它是一个允许通过交换诸如字典之类的结构来管理进程的接口。由于遗传,iOS 5 也具备这种机制。

你可能已经明白我所说的这个介绍的意思了。是的,iOS 中有包含 XPC 通信工具的系统服务。我想用一个用于 SMS 发送的守护进程来举例说明这项工作。不过需要说明的是,这个能力在 iOS 6 中是固定的,但与 iOS 5.0—5.1.1 相关。越狱、私有框架和其他非法工具都不需要它的利用。只需要目录 /usr/include/xpc/* 中的一组头文件。

iOS 中短信发送的元素之一是系统服务 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");
});

就是这样。已发送短信。

【讨论】:

  • 您不应该使用 XPC 发送短信。制作的 App 不会获得 Apple 的批准。改用 MessageUI 框架
  • 如果您将此技术用于您的组织使用的应用程序,并且应用程序没有通过应用程序商店,您不必担心。
  • 在 iOS 12 上出现“XPC 沙盒连接错误:连接无效”
【解决方案6】:

添加 MessageUI.Framework 并使用以下代码

#import <MessageUI/MessageUI.h> 

然后:

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];
}

和委托方法 -

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
             didFinishWithResult:(MessageComposeResult)result {
      [self dismissViewControllerAnimated:YES completion:nil];
 }

【讨论】:

  • 嘿,太好了,但我们可以从后台执行此功能吗?
  • Apple 不允许您在未经用户批准的情况下发送消息。他必须通过手动按下按钮来发送消息/邮件。或者,您可以通过将电子邮件/号码发送到您的后端然后发送来使用自定义服务。虽然你不能直接在 iPhone 上这样做
【解决方案7】:

您可以使用这种方法:

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:MobileNumber"]]


iOS 会自动从您的应用导航到消息应用的消息撰写页面。由于 URL 的方案以 sms: 开头,因此这被识别为消息应用程序识别并启动它的类型。

【讨论】:

    【解决方案8】:

    遵循此程序

    1.将MessageUI.Framework添加到项目

    2 。在 .h 文件中导入#import &lt;MessageUI/MessageUI.h&gt;

    3 .复制此代码以发送消息

     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 方法。

    - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    
       ///your stuff here 
    
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    

    奔跑吧!

    【讨论】:

    • 值得一提的是,您可能希望在messageComposeViewController: didFinishWithResult: 回调方法中运行[self dismissViewControllerAnimated:YES completion:nil];。否则它会挂在那里。
    【解决方案9】:
    //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]; 
    }
    

    【讨论】:

    【解决方案10】:

    这是在 iOS 中发送 SMS 的 Swift 版本代码。请注意,它仅适用于真实设备。在 iOS 7+ 中测试的代码。你可以阅读更多here

    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.")
        }
    }
    

    【讨论】:

      【解决方案11】:

      iOS 4 中有一个类支持从您的应用程序发送带有正文和配方的消息。它的工作原理与发送邮件相同。你可以在这里找到文档:link text

      【讨论】:

        【解决方案12】:
        - (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
        {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            UIImage *ui =resultimg.image;
            pasteboard.image = ui;
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]];
        }
        

        【讨论】:

        • 你的参数有多大用处?
        • [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]];
        【解决方案13】:

        //用名字和号码调用方法。

        -(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"];
        }
        

        }

        //发送消息的方法

        - (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];
         }
        }
        

        【讨论】:

          【解决方案14】:

          如果你愿意,你可以使用私有框架CoreTelephony,它调用了CTMessageCenter类。有几种发送短信的方法。

          【讨论】:

          • 他特别询问是否可以使用官方SDK。
          • 你能提供更多关于私有api的信息吗?我使用私有框架没有问题,因为我不需要将它发布到 App Store。
          【解决方案15】:

          使用这个:

          - (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];
          }
          

          【讨论】:

            【解决方案16】:
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:number"]] 
            

            这将是最好的方法。

            【讨论】:

              【解决方案17】:

              您可以展示 MFMessageComposeViewController,它可以发送短信,但有用户提示(他点击发送按钮)。没有用户许可,没有办法做到这一点。在 iOS 11 上,您可以进行扩展,就像过滤传入消息一样,告诉 iOS 是否是垃圾邮件。短信无所不能

              【讨论】:

              • 新鲜的答案!
              【解决方案18】:

              如果您想在自己的应用中显示创建和发送消息,则需要使用 MFMessageComposeViewController

              否则,您可以使用 sharedApplication 方法。

              【讨论】:

                猜你喜欢
                • 2014-05-09
                • 1970-01-01
                • 2011-04-09
                • 1970-01-01
                • 2011-04-15
                • 1970-01-01
                • 1970-01-01
                • 2011-11-16
                相关资源
                最近更新 更多