Swift Extension with delegation (UIViewController)

I need ability to send email in a number of view controllers in my app. The code is same, take three params -- recipient address, body, and subject. If Mail is configured on device, initialize MFMailComposeViewController with the view controller as delegate. If Mail is not configured, throw an error. Also set current view controller as mailComposeDelegate to listen to callbacks. How does one use Swift extension to achieve it (setting delegate in extension being the main issue)?

1 answer

  • answered 2018-11-13 05:18 Alex Bailey

    Create a global function:

    func sendEmail(address: String, body: String, subject: String, viewController: UIViewController) {
        //check if email is configured or throw error...
    }