Hi im new at developing and i got 2 errors i can't seem to find an answer on google. here is my code and the errors are
Cannot find 'self' in scope
Cannot find 'present' in scope
//
// ViewController.swift
// Diabell
//
// Created by Richard Klug on 14/04/2021.
//
import UIKit
import MessageUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func emailbButtonTapped( sender: Any) {
showMailComposer()
}
}
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else{
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["richard.klug@diabell.se"])
composer.setSubject("Diabell App Help")
composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false)
present(composer, animated: true)
}
extension ViewController: MFMailComposeViewControllerDelegate {
func mailComposeController( controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
controller.dismiss(animated: true)
return
}
switch result {
case .cancelled:
print ("Cancelled")
case .failed:
print ("Failed to send")
case .saved:
print ("Saved")
case .sent:
print ("Email Sent")
}
controller.dismiss(animated: true)
}
}
Cannot find 'self' in scope
Cannot find 'present' in scope
//
// ViewController.swift
// Diabell
//
// Created by Richard Klug on 14/04/2021.
//
import UIKit
import MessageUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func emailbButtonTapped( sender: Any) {
showMailComposer()
}
}
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else{
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["richard.klug@diabell.se"])
composer.setSubject("Diabell App Help")
composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false)
present(composer, animated: true)
}
extension ViewController: MFMailComposeViewControllerDelegate {
func mailComposeController( controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
controller.dismiss(animated: true)
return
}
switch result {
case .cancelled:
print ("Cancelled")
case .failed:
print ("Failed to send")
case .saved:
print ("Saved")
case .sent:
print ("Email Sent")
}
controller.dismiss(animated: true)
}
}
You have 2 errors :
Correct code:
If that's OK, don't forget to mark the correct answer to close the thread.
Note: when you post code, please use the code formatter tool (<>).
Code Block // // ViewController.swift // Diabell // // Created by Richard Klug on 14/04/2021. // import UIKit import MessageUI class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func emailbButtonTapped( sender: Any) { showMailComposer() } } func showMailComposer() { guard MFMailComposeViewController.canSendMail() else { return } let composer = MFMailComposeViewController() composer.mailComposeDelegate = self composer.setToRecipients(["richard.klug@diabell.se"]) composer.setSubject("Diabell App Help") composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false) present(composer, animated: true) }
need to conform to MFMailComposeViewControllerDelegate
Code Block class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
Extra closing curly bracket line 21: remove it and put it line 34
Correct code:
Code Block class ViewController: UIViewController, MFMailComposeViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func emailbButtonTapped( sender: Any) { showMailComposer() } func showMailComposer() { guard MFMailComposeViewController.canSendMail() else{ return } let composer = MFMailComposeViewController() composer.mailComposeDelegate = self composer.setToRecipients(["richard.klug@diabell.se"]) composer.setSubject("Diabell App Help") composer.setMessageBody("Fyll i vad du behöver hjälp med", isHTML: false) present(composer, animated: true) } }
If that's OK, don't forget to mark the correct answer to close the thread.
Note: when you post code, please use the code formatter tool (<>).