我有一个简单的应用程序。它通过电子邮件和短信/彩信截屏。
因为我有两个按钮,每个按钮执行两个不同的功能 - (1)截图然后发送电子邮件; (2)截图然后是短信/彩信,我需要在我的代码中将这些添加到我的class
。
目前......
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
如何将MFMessageComposeViewControllerDelegate
添加到我上面的类声明中?
我认为我需要具备发送电子邮件和短信/彩信的功能。
为我的语言描述道歉,我很新:)
答案 0 :(得分:1)
另请查看此链接:check this link
import UIKit
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate {
@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
}
@IBAction func sendMail(sender: UIButton)
{
let mailClass:AnyClass? =NSClassFromString("MFMailComposeViewController")
if(mailClass != nil)
{
if((mailClass?.canSendMail()) != nil)
{
displayComposerSheet()
}
}
func displayComposerSheet()
{
let picker: MFMailComposeViewController=MFMailComposeViewController()
picker.mailComposeDelegate=self;
picker .setSubject("Test")
picker.setMessageBody("Mail Sharing !", isHTML: true)
let data: NSData = UIImagePNGRepresentation(UIImage(named: "images.jpg")!)!
picker.addAttachmentData(data, mimeType: "image/png", fileName: "images.png")
self.presentViewController(picker, animated: true, completion: nil)
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result.rawValue {
case MFMailComposeResultCancelled.rawValue:
print("Mail cancelled")
case MFMailComposeResultSaved.rawValue:
print("Mail saved")
case MFMailComposeResultSent.rawValue:
print("Mail sent")
case MFMailComposeResultFailed.rawValue:
print("Mail sent failure: \(error!.localizedDescription)")
default:
break
}
controller.dismissViewControllerAnimated(true, completion: nil)
}
}