繁体   English   中英

在iPhone中以编程方式发送短信

[英]sending sms programmatically in iphone

如何通过编程方式将短信发送到iPhone中联系人列表中选择的特定号码?

MFMessageComposeController是您想要的。

要发送短信,您正在查看以下内容:

#import <MessageUI/MessageUI.h>
@interface myClass : NSObject <MFMessageComposeViewControllerDelegate>{
}
@end

@implementation

-(void)sendMessage{
    if([MFMessageComposeController canSendText]){
        MFMessageComposeController *smsComposer =
[[MFMessageComposeController alloc] init]; smsComposer.recipients = [NSArray arrayWithObject:@"12345678"]; smsComposer.body = @"SMS BODY HERE"; smsComposer.delegate = self; [self presentModalViewController:smsComposer animated:NO]; } else{ //You probably want to show a UILocalNotification here. } } - (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result{ /* You can use the MessageComposeResult to determine what happened to the
message. I believe it tells you about sent, stored for sending later, failed
or cancelled. */ [self dismissModalViewControllerAnimated:NO]; } @end

目前,这是从您的应用发送短信的唯一方法。 除非您只想打开SMS应用程序。 如果您不担心邮件的正文,可以执行以下操作:

NSString *smsURL = @"sms:12345678";
NSURL *url = [NSURL URLWithString:smsURL];
[[UIApplication sharedApplication] openURL:url];

嗯...我认为在这里进行一些讨论会有所帮助。 我(可能是错误地)提出了一个问题,“ ...以编程方式发送SMS ...”是指在幕后发送SMS,然后弹出MFMessageComposeViewController。

如果这是问题所在,则以上答案的绿色复选标记是不正确的。 我将假设这就是问题所在(我敢打赌我不是唯一的一个),并提供一些子弹来节省其他人花在我这里的时间。

  1. 堆栈中有一些安静的讨论 ,认为这无法在iOS中完成。 在这里
  2. 适用于Android的cordova插件就可以了
  3. iOS的cordova插件不支持(暗示无法完成。)
  4. 上面的代码无法运行。 它是一种伪代码。 presentModalViewController上的animation:NO阻止了vc的弹出,但是我总是以MessageCancelled结尾于didFinishWithResult。
  5. 苹果公司有权阻止这种情况发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM