用Swift在iOS中发送短信

首先,我真的很惊讶这不是重复的,因为在Objective-C中有很多解决这个问题的stackoverflow问题,但是我还没有看到使用Swift的很好的答案。

我在找的是Swift中的代码片段,它将任意string作为文本消息的主体发送给给定的电话号码。 从本质上讲,我想从苹果官方文档中得到类似的东西,但是在Swift中而不是在Objective-C中。

我想这不是太困难,因为它可以在Android的几行代码中完成。

编辑:我正在寻找的是5 – 20行的斯威夫特代码,我不同意这是太宽泛。 在Java(对于Android)中,解决scheme如下所示:

package com.company.appname; import android.app.Activity; import android.telephony.SmsManager; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); public static final mPhoneNumber = "1111111111"; public static final mMessage = "hello phone"; SmsManager.getDefault().sendTextMessage(mPhoneNumber, null, mMessage, null, null); } } 

现在这是android解决scheme,它只有11行。 Java倾向于比Swift更详细,所以我怀疑我所要求的是“太宽泛”,更可能是我不知道如何使用Objective-C MessageComposer对象,因为我链接的文档至于以上在Swift中的使用方面还不清楚。

不知道你是否真的得到了答案。 我遇到类似的狩猎,遇到了这个解决scheme,并得到它的工作。

 import UIKit import MessageUI class ViewController: UIViewController, MFMessageComposeViewControllerDelegate { @IBOutlet weak var phoneNumber: UITextField! override func viewDidLoad() { super.viewDidLoad() } @IBAction func sendText(sender: UIButton) { if (MFMessageComposeViewController.canSendText()) { let controller = MFMessageComposeViewController() controller.body = "Message Body" controller.recipients = [phoneNumber.text] controller.messageComposeDelegate = self self.presentViewController(controller, animated: true, completion: nil) } } func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) { //... handle sms screen actions self.dismissViewControllerAnimated(true, completion: nil) } override func viewWillDisappear(animated: Bool) { self.navigationController?.navigationBarHidden = false } } 

Swift 3.0解决scheme:

 func sendSMSText(phoneNumber: String) { if (MFMessageComposeViewController.canSendText()) { let controller = MFMessageComposeViewController() controller.body = "" controller.recipients = [phoneNumber] controller.messageComposeDelegate = self self.present(controller, animated: true, completion: nil) } } func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { //... handle sms screen actions self.dismiss(animated: true, completion: nil) } override func viewWillDisappear(_ animated: Bool) { self.navigationController?.isNavigationBarHidden = false } 

Swift 3

 @IBAction func sendSmsClick(_ sender: AnyObject) { let messageVC = MFMessageComposeViewController() messageVC.body = "Enter a message"; messageVC.recipients = ["Enter tel-nr"] messageVC.messageComposeDelegate = self; self.present(messageVC, animated: false, completion: nil) } func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { switch (result.rawValue) { case MessageComposeResult.cancelled.rawValue: print("Message was cancelled") self.dismiss(animated: true, completion: nil) case MessageComposeResult.failed.rawValue: print("Message failed") self.dismiss(animated: true, completion: nil) case MessageComposeResult.sent.rawValue: print("Message was sent") self.dismiss(animated: true, completion: nil) default: break; } } 

如果你不想依赖一个UIViewController,请遵循一个Swift 3.0解决scheme:

 import UIKit import MessageUI class ECMMessageComposerBuilder: NSObject { private dynamic var customWindow: UIWindow? private var body: String? private var phoneNumber: String? fileprivate var messageController: MFMessageComposeViewController? var canCompose: Bool { return MFMessageComposeViewController.canSendText() } func body(_ body: String?) -> ECMMessageComposerBuilder { self.body = body return self } func phoneNumber(_ phone: String?) -> ECMMessageComposerBuilder { self.phoneNumber = phone return self } func build() -> UIViewController? { guard canCompose else { return nil } messageController = MFMessageComposeViewController() messageController?.body = body if let phone = phoneNumber { messageController?.recipients = [phone] } messageController?.messageComposeDelegate = self return messageController } func show() { customWindow = UIWindow(frame: UIScreen.main.bounds) customWindow?.rootViewController = MNViewController() // Move it to the top let topWindow = UIApplication.shared.windows.last customWindow?.windowLevel = (topWindow?.windowLevel ?? 0) + 1 // and present it customWindow?.makeKeyAndVisible() if let messageController = build() { customWindow?.rootViewController?.present(messageController, animated: true, completion: nil) } } func hide(animated: Bool = true) { messageController?.dismiss(animated: animated, completion: nil) messageController = nil customWindow?.isHidden = true customWindow = nil } } extension ECMMessageComposerBuilder: MFMessageComposeViewControllerDelegate { func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { controller.dismiss(animated: true, completion: nil) hide() } } 

你这样称呼composer php:

 let phoneNumber = "987654321" let composer = MNMessageComposerBuilder() composer.phoneNumber(phoneNumber).show() 

或使用一个懒惰的变种

 let phoneNumber = "987654321" private lazy var messageComposer: MNMessageComposerBuilder = { let composer = MNMessageComposerBuilder() return composer }() messageComposer.phoneNumber(phoneNumber).show() 
 @IBAction func sendMessageBtnClicked(sender: AnyObject) { var messageVC = MFMessageComposeViewController() messageVC.body = "Enter a message"; messageVC.recipients = ["Enter tel-nr"] messageVC.messageComposeDelegate = self; self.presentViewController(messageVC, animated: false, completion: nil) } func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) { switch (result.value) { case MessageComposeResultCancelled.value: println("Message was cancelled") self.dismissViewControllerAnimated(true, completion: nil) case MessageComposeResultFailed.value: println("Message failed") self.dismissViewControllerAnimated(true, completion: nil) case MessageComposeResultSent.value: println("Message was sent") self.dismissViewControllerAnimated(true, completion: nil) default: break; } }