有没有人知道,通过官方SDK / Cocoa Touch,从iPhone发送短信是否可行,以及如何实现?
当前回答
用这个:
- (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];
}
其他回答
如果你想在你自己的应用程序中显示创建和发送消息,你需要使用MFMessageComposeViewController。
否则,您可以使用sharedApplication方法。
您可以使用sms:[目标电话号码]URL打开sms应用程序,但没有指示如何用文本预填充sms正文。
限制
如果你可以在iPhone的程序中发送短信,你就可以在后台编写垃圾邮件游戏。我相信你一定很想收到朋友的垃圾邮件,“试试这款新游戏!它撕破了我的短裤,你的也会!roxxersboxxers.com ! !如果你现在注册,你将得到3200个RB点!!”
苹果对自动(甚至部分自动)短信和拨号操作有限制。(想象一下如果游戏在一天的特定时间拨打911)
最好的办法是在互联网上设置一个使用在线SMS发送服务的中间服务器,如果需要完全自动化,则通过该路由发送SMS。(例如,iPhone上的程序向服务器发送UDP数据包,服务器发送真正的短信)
iOS 4更新
然而,iOS 4现在提供了一个viewController,你可以导入到你的应用程序中。预先填充SMS字段,然后用户可以在控制器内发起SMS发送。与使用“SMS:…”url格式不同,这允许您的应用程序保持打开状态,并允许您填充to和body字段。您甚至可以指定多个收件人。
这可以防止应用程序在用户没有明确意识到的情况下发送自动SMS。你仍然不能从iPhone本身发送完全自动化的短信,它需要一些用户交互。但这至少允许您填充所有内容,并避免关闭应用程序。
MFMessageComposeViewController类有很好的文档,并且教程展示了它的实现是多么容易。
iOS 5更新
iOS 5包括iPod touch和iPad设备的消息,所以虽然我还没有亲自测试,但可能所有iOS设备都可以通过MFMessageComposeViewController发送短信。如果是这样的话,那么苹果正在运行一个SMS服务器,它代表没有蜂窝调制解调器的设备发送消息。
iOS 6更新
这个类没有变化。
iOS 7更新
您现在可以检查您正在使用的消息媒体是否接受主题或附件,以及它将接受哪种附件。您可以编辑主题并在媒体允许的情况下向邮件添加附件。
iOS 8更新
这个类没有变化。
iOS 9更新
这个类没有变化。
iOS 10更新
这个类没有变化。
iOS 11更新
这个类没有显著的变化
该类的限制
请记住,这在没有iOS 4的手机上是行不通的,在iPod touch或iPad上也行不通,除非是在iOS 5下。在使用这个控制器之前,你必须检测设备和iOS的限制,否则你的应用可能会被限制在最近升级的3G、3GS和4部iphone上。
然而,发送短信的中间服务器将允许任何和所有这些iOS设备发送短信,只要他们有互联网接入,所以它可能仍然是许多应用程序的一个更好的解决方案。或者,两者都使用,只有在设备不支持在线短信服务时才使用在线短信服务。
如果您愿意,您可以使用名为CTMessageCenter类的私有框架CoreTelephony。有一些发送短信的方法。
遵循以下步骤
1 .添加消息i。项目框架
2。在.h文件中导入# Import <MessageUI/MessageUI.h>。
3.复制此代码以发送消息
if ([MFMessageComposeViewController canSendText]) {
MFMessageComposeViewController *messageComposer =
[[MFMessageComposeViewController alloc] init];
NSString *message = @"Message!!!";
[messageComposer setBody:message];
messageComposer.messageComposeDelegate = self;
[self presentViewController:messageComposer animated:YES completion:nil];
}
4所示。实现委托方法,如果你想。
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
///your stuff here
[self dismissViewControllerAnimated:YES completion:nil];
}
快跑!
推荐文章
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?
- 如何从iPhone访问SOAP服务