sidlatau / flutter_email_sender

Allows send emails from flutter using native platform functionality.
Apache License 2.0
151 stars 84 forks source link

Recommend new Feature: hasProvider() => Boolean #89

Open fischey007 opened 1 year ago

fischey007 commented 1 year ago

Before the user send his message or attachements to a mail provider, the app should check if there is any mail provider available. If the mail provider is not available, we don't need to trigger the method 'send' because it will not executed successfully:

i have implemented a solution for android and iOS which is similiar to actual plugin code:

android function:

 private fun hasProvider(callback: Result){
        if (activity == null) {
            callback.success(false)
            return
        }
        val intent = Intent()
        intent.action = Intent.ACTION_SENDTO
        intent.data = Uri.parse("mailto:")
        val packageManager = activity?.packageManager
        if (packageManager?.resolveActivity(intent, 0) != null) {
            callback.success(true)
        } else {
            callback.success(false)
        }
}

iOS function:

private func hasProvider(result: @escaping FlutterResult){
        guard let viewController = UIApplication.shared.keyWindow?.rootViewController else {
            result(false)
            return
        }
        if MFMailComposeViewController.canSendMail() {
            result(true)
        } else {
            result(false)
        }
    }