发送短信 iPhone

发布于 2024-12-18 16:44:34 字数 608 浏览 2 评论 0原文

我试图从我的应用程序内发送短信。我写了这段代码,但似乎不起作用。 没有崩溃,没有错误日志,什么也没有发生(当然我尝试记录canSendText,结果是1)。

- (void)viewDidLoad
{
    [super viewDidLoad];

    messageComposer = [[MFMessageComposeViewController alloc] init];

    if ([MFMessageComposeViewController canSendText]) {

        [messageComposer setBody:@"Messaggio generato da SMSTest"];
        [messageComposer setRecipients:[NSArray arrayWithObject:@"3333333333"]];
        [messageComposer setDelegate:self];

        [self presentModalViewController:messageComposer animated:YES];
    }
}

谁能解释一下我做错了什么?

I was trying to send SMS from within my app. I wrote this piece of code but it seems not to work.
No crash, no error log, simply nothing happen (of course I tried to log canSendText and the result is 1).

- (void)viewDidLoad
{
    [super viewDidLoad];

    messageComposer = [[MFMessageComposeViewController alloc] init];

    if ([MFMessageComposeViewController canSendText]) {

        [messageComposer setBody:@"Messaggio generato da SMSTest"];
        [messageComposer setRecipients:[NSArray arrayWithObject:@"3333333333"]];
        [messageComposer setDelegate:self];

        [self presentModalViewController:messageComposer animated:YES];
    }
}

Can anyone explain me what I'm doing wrong?

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

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

发布评论

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

评论(4

梦太阳 2024-12-25 16:44:34

问题是 presentModalViewController 无法在 viewDidLoad 中工作,因为视图已加载,但可能还没有出现在屏幕上。如果您将代码放入 viewWillAppear:animated 中,这应该可以工作。

编辑:根据 Saphrosit 的评论:viewDidAppear: 是一个更好的地方。

The problem is that presentModalViewController does not work in viewDidLoad yet as the view is loaded but might not even be on screen yet. If you put your code in viewWillAppear:animated, this should work.

Edit: As per Saphrosit's comment: viewDidAppear: is an even better place to do this.

吖咩 2024-12-25 16:44:34

我成功地使用了它:

MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.messageComposeDelegate = self;
controller.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:controller animated:YES];

请注意,它是 messageComposeDelegate,而不是像您那样的“委托”。

蒂姆

I use this successfully:

MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.messageComposeDelegate = self;
controller.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:controller animated:YES];

Note that it's messageComposeDelegate, not 'delegate' as you do.

Tim

少跟Wǒ拽 2024-12-25 16:44:34

当我在模拟器上尝试代码时,我收到一个 UIAlert 消息,说短信不可用,因为模拟器无法发送消息。您是否检查过您的头文件是 MFMessageComposeViewControllerDelegate 的委托?

YourClassName : UIViewController  <MFMessageComposeViewControllerDelegate>

When i try the code on simulator i get an UIAlert saying text messaging is not available, because simulator can´t send messages. Have you checked that your header file is a delegate of MFMessageComposeViewControllerDelegate ?

YourClassName : UIViewController  <MFMessageComposeViewControllerDelegate>
如梦亦如幻 2024-12-25 16:44:34

//试试这个...它将运行..

MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
      if([MFMessageComposeViewController canSendText])
      {
          controller.body = @"来自卡蒂克的你好";
          controller.recipients = [NSArray arrayWithObjects:@"12356478", nil];
          控制器.messageComposeDelegate = self;
          [自我呈现ModalViewController:控制器动画:是];
      }

//try this ... it will run ..

MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
      if([MFMessageComposeViewController canSendText])
      {
          controller.body = @"Hello from Kartik";
          controller.recipients = [NSArray arrayWithObjects:@"12356478", nil];
          controller.messageComposeDelegate = self;
          [self presentModalViewController:controller animated:YES];
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文