发送短信 iPhone
我试图从我的应用程序内发送短信。我写了这段代码,但似乎不起作用。 没有崩溃,没有错误日志,什么也没有发生(当然我尝试记录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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是
presentModalViewController
无法在viewDidLoad
中工作,因为视图已加载,但可能还没有出现在屏幕上。如果您将代码放入viewWillAppear:animated
中,这应该可以工作。编辑:根据 Saphrosit 的评论:
viewDidAppear:
是一个更好的地方。The problem is that
presentModalViewController
does not work inviewDidLoad
yet as the view is loaded but might not even be on screen yet. If you put your code inviewWillAppear:animated
, this should work.Edit: As per Saphrosit's comment:
viewDidAppear:
is an even better place to do this.我成功地使用了它:
请注意,它是 messageComposeDelegate,而不是像您那样的“委托”。
蒂姆
I use this successfully:
Note that it's messageComposeDelegate, not 'delegate' as you do.
Tim
当我在模拟器上尝试代码时,我收到一个 UIAlert 消息,说短信不可用,因为模拟器无法发送消息。您是否检查过您的头文件是 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 ?
//试试这个...它将运行..
//try this ... it will run ..