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

发布于 2024-10-15 16:50:29 字数 41 浏览 2 评论 0原文

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

How can you send an SMS programmatically to a particular number selected in the contact list in iPhone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-10-22 16:50:29

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

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

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

MFMessageComposeController is what you're looking for.

To send an SMS, you're looking at something like this:

#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

That's the only way to send SMSs from your app at the moment. Unless you just want to open the SMS app. If you aren't worried about the body of the message, you can do this:

NSString *smsURL = @"sms:12345678";
NSURL *url = [NSURL URLWithString:smsURL];
[[UIApplication sharedApplication] openURL:url];
三生池水覆流年 2024-10-22 16:50:29

呃...我认为在这里进行一些讨论会有所帮助。
我(可能错误地)认为“...以编程方式发送短信...”的意思是在幕后发送短信,然后 MFMessageComposeViewController 弹出。

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

  1. 有一点安静 堆栈中的讨论表明这在 iOS 中无法完成。和此处
  2. cordova插件for Android
  3. Cordova 插件for iOS 不行(暗示做不到。)
  4. 上面的代码是没有办法的运行。它是一种伪代码。 PresentModalViewController 上的动画:NO 阻止 vc 弹出,但我总是以 didFinishWithResult 和 MessageCancelled 结束。
  5. 苹果公司阻止这种情况的发生是正确的。

Uh... I think a little discussion would be helpful here.
I (maybe mistakenly) take the question, "... send an SMS programmatically ..." to mean sending SMS behind the scenes, w o the MFMessageComposeViewController popping up.

The green checkmark for the answer above is incorrect IF that's the question. I am going to assume that that is the question (I bet I'm not the only one) and offer some bullets to save others the time I have spent getting here.

  1. There is quiet a bit of discussion in stack that this cannot be done in iOS. and here
  2. The cordova plugin for Android does it just fine
  3. the cordova plugin for iOS does not (implying that it cant be done.)
  4. There is no way the code above runs. Its a sort of pseudo-code. The animated:NO on presentModalViewController, prevents the vc from popping up, but I always end up at didFinishWithResult with MessageCancelled.
  5. Its right in character for Apple to prevent this.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文