有没有人知道,通过官方SDK / Cocoa Touch,从iPhone发送短信是否可行,以及如何实现?
当前回答
下面是在iOS中发送短信的Swift版本代码。请注意,它只在实际设备中工作。在iOS 7+中测试的代码。你可以在这里阅读更多。
1)创建一个继承MFMessageComposeViewControllerDelegate和NSObject的新类:
import Foundation
import MessageUI
class MessageComposer: NSObject, MFMessageComposeViewControllerDelegate {
// A wrapper function to indicate whether or not a text message can be sent from the user's device
func canSendText() -> Bool {
return MFMessageComposeViewController.canSendText()
}
// Configures and returns a MFMessageComposeViewController instance
func configuredMessageComposeViewController(textMessageRecipients:[String] ,textBody body:String) -> MFMessageComposeViewController {
let messageComposeVC = MFMessageComposeViewController()
messageComposeVC.messageComposeDelegate = self // Make sure to set this property to self, so that the controller can be dismissed!
messageComposeVC.recipients = textMessageRecipients
messageComposeVC.body = body
return messageComposeVC
}
// MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it
func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
controller.dismissViewControllerAnimated(true, completion: nil)
}
}
2)如何使用这个类:
func openMessageComposerHelper(sender:AnyObject ,withIndexPath indexPath: NSIndexPath) {
var recipients = [String]()
//modify your recipients here
if (messageComposer.canSendText()) {
println("can send text")
// Obtain a configured MFMessageComposeViewController
let body = Utility.createInvitationMessageText()
let messageComposeVC = messageComposer.configuredMessageComposeViewController(recipients, textBody: body)
// Present the configured MFMessageComposeViewController instance
// Note that the dismissal of the VC will be handled by the messageComposer instance,
// since it implements the appropriate delegate call-back
presentViewController(messageComposeVC, animated: true, completion: nil)
} else {
// Let the user know if his/her device isn't able to send text messages
self.displayAlerViewWithTitle("Cannot Send Text Message", andMessage: "Your device is not able to send text messages.")
}
}
其他回答
限制
如果你可以在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设备发送短信,只要他们有互联网接入,所以它可能仍然是许多应用程序的一个更好的解决方案。或者,两者都使用,只有在设备不支持在线短信服务时才使用在线短信服务。
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
UIImage *ui =resultimg.image;
pasteboard.image = ui;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]];
}
遵循以下步骤
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];
}
快跑!
如果你想在你自己的应用程序中显示创建和发送消息,你需要使用MFMessageComposeViewController。
否则,您可以使用sharedApplication方法。
//Add the Framework in .h file
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
//Set the delegate methods
UIViewController<UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate>
//add the below code in .m file
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
MFMessageComposeViewController *controller =
[[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
NSString *str= @"Hello";
controller.body = str;
controller.recipients = [NSArray arrayWithObjects:
@"", nil];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:
(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
switch (result)
{
case MessageComposeResultCancelled:
NSLog(@"Cancelled");
break;
case MessageComposeResultFailed:
NSLog(@"Failed");
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
推荐文章
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?
- 如何从iPhone访问SOAP服务