有没有人知道,通过官方SDK / Cocoa Touch,从iPhone发送短信是否可行,以及如何实现?
当前回答
如果您愿意,您可以使用名为CTMessageCenter类的私有框架CoreTelephony。有一些发送短信的方法。
其他回答
您可以使用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设备发送短信,只要他们有互联网接入,所以它可能仍然是许多应用程序的一个更好的解决方案。或者,两者都使用,只有在设备不支持在线短信服务时才使用在线短信服务。
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms:number"]]
这将是最好和最简单的方法。
用这个:
- (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];
}
//使用名称和数字调用方法。
-(void)openMessageViewWithName:(NSString*)contactName withPhone:(NSString *)phone{
CTTelephonyNetworkInfo *networkInfo=[[CTTelephonyNetworkInfo alloc]init];
CTCarrier *carrier=networkInfo.subscriberCellularProvider;
NSString *Countrycode = carrier.isoCountryCode;
if ([Countrycode length]>0) //Check If Sim Inserted
{
[self sendSMS:msg recipientList:[NSMutableArray arrayWithObject:phone]];
}
else
{
[AlertHelper showAlert:@"Message" withMessage:@"No sim card inserted"];
}
}
//发送消息的方法
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSMutableArray *)recipients{
MFMessageComposeViewController *controller1 = [[MFMessageComposeViewController alloc] init] ;
controller1 = [[MFMessageComposeViewController alloc] init] ;
if([MFMessageComposeViewController canSendText])
{
controller1.body = bodyOfMessage;
controller1.recipients = recipients;
controller1.messageComposeDelegate = self;
[self presentViewController:controller1 animated:YES completion:Nil];
}
}
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- Swift:声明一个空字典
- 为什么ARC仍然需要@autoreleasepool ?
- 在成功提交我的应用程序后,“太多符号文件”
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 如何从UIImage (Cocoa Touch)或CGImage (Core Graphics)获取像素数据?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- Xcode 4挂在“附加到(应用程序名称)”
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0