如何在iPhone上编程发送短信?

545

请问有人知道如何通过官方的SDK / Cocoa Touch在上以编程方式发送SMS吗?


在iOS 4中,你可以用代码发送短信,但问题是你的应用程序将会被关闭。 - Pankaj Kainthla
如果其他人不需要使用官方SDK,我写了这篇文章来展示如何使用Twilio完成此操作:https://www.twilio.com/blog/2016/11/how-to-send-an-sms-from-ios-in-swift.html - Megan Speir
您可以通过Gmail创建Google电子表格,制定所需字段,然后在“工具”中选择“脚本编辑器”,使用POST请求调用“MailApp.sendEmail” API向手机号发送电子邮件。AT&T的是YOURNUMBER@mms.att.net,T-Mobile的是YOURNUMBER@tmomail.net(全部免费)。 - Coty Embry
18个回答

3

//使用名称和数字调用方法。

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

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

4
你的参数有多实用? - martinsurleweb
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]]; - Amr Angry

3

iOS 4中有一个类,支持从应用程序发送带有正文和收件人的消息。它的工作方式与发送电子邮件相同。您可以在此处找到文档:链接文本


2

如果您想要,可以使用私有框架 CoreTelephony,其中包含 CTMessageCenter 类。有几种方法可以发送短信。


1
他特别问是否可以使用官方 SDK 实现这一点。 - Quentamia
1
你能提供更多关于私有API的信息吗?我没有使用私有框架的问题,因为我不需要将其发布到应用商店。 - Bagusflyer

1

请使用此功能:

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

1
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:number"]] 

这将是实现它的最佳且简短的方式。


1
您可以使用MFMessageComposeViewController来发送短信,但需要用户点击“发送”按钮以获得权限。没有未经用户许可的方法可以完成此操作。在iOS 11上,您可以创建扩展程序,用作过滤器,判断接收到的消息是垃圾短信还是正常消息。除此之外,无法对短信进行其他操作。

0

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接