Miles
Miles

Reputation: 625

Cannot convert value of type 'UIAlertControllerStyle.Type' to expected argument type 'UIAlertControllerStyle'

I have a UIButton on my app that displays a UIAlertView when tapped. The alert view will contain a button that opens the iOS email compose view.

I have the mail compose view working great, but when the user sends an email or hits "cancel", the mail compose view doesn't go away. The code I'm using doesn't seem to work as I am getting this error:

Cannot convert value of type 'UIAlertControllerStyle.Type' to expected argument type 'UIAlertControllerStyle'

            var alert = UIAlertController(title: "Alert", message: "Your Device cannot send emails", preferredStyle: UIAlertControllerStyle)

What might be going on here? Thanks!

var myMail: MFMailComposeViewController!

@IBAction func helpfeedbackAlert(_ sender: Any) {

    if(MFMailComposeViewController.canSendMail()){
        myMail = MFMailComposeViewController()

        myMail.setSubject("Test")
        myMail.setToRecipients(["[email protected]"])

        self.present(myMail, animated: true, completion: nil)
    }
    else{
        var alert = UIAlertController(title: "Alert", message: "Your Device cannot send emails", preferredStyle: UIAlertControllerStyle)
        self.present(alert, animated: true, completion: nil)

    }

}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.          
}

func mailComposeController(controller: MFMailComposeViewController!, didFinishWith: MFMailComposeResult, error: NSError!){

    switch result.rawValue {

    case MFMailComposeResult.cancelled.rawValue:
        print("Mail cancelled")

    case MFMailComposeResult.sent.rawValue:
        print("Your email has been sent!")

    case MFMailComposeResult.failed.rawValue:
        print("Email has failed to send: %@", [error!.localizedDescription])
    default:
        break

    }

    // Dismiss the mail compose view controller
    controller.dismiss(animated: true, completion: nil)


}

Upvotes: 1

Views: 876

Answers (2)

Vijay Ladva
Vijay Ladva

Reputation: 215

var alert = UIAlertController(title: "Alert", message: "Your Device cannot send emails", preferredStyle: .Alert)

Upvotes: 1

rmaddy
rmaddy

Reputation: 318924

The alert issue is simple. You need to specify a specific alert style (.alert) but you are passing the name of the enum instead of passing a specific value.

The problem with the mail composer not dismissing is also simple. You never set the mailComposeDelegate property.

The problem with the switch is that you have the wrong method signature. It needs to be:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)

And there's no need for all of the rawValue usage.

Upvotes: 3

Related Questions