dvkch / SYEmailHelper

Easy way to detect installed email clients and allow the user to choose one when sending an email
Other
2 stars 3 forks source link

SYEmailHelper

Easily detect installed email clients and allow the user to choose one when sending an email.

I use it to quickly add a "Contact me" feature in my apps. This will detect a list of SYEmailService, representing:

If there are no items in the list the completion block is called with an error: SYEmailHelperErrorDomain, code SYEmailHelperErrorCode_NoServiceAvailable.

If the use selected a third party app that couldn't be opened an error will be generated too: SYEmailHelperErrorCode_CouldntOpenApp.

If a single supported service is available it will automatically be launched.

If multiple supported services are available an action sheet will be displayed.

You can:

Example:

    PasteboardEmailService.name = "Copy email address to pasteboard"

    EmailHelper.shared.showCopyToPasteBoard = true
    EmailHelper.shared.actionSheetTitle = "Which app you wanna use?"
    EmailHelper.shared.actionSheetMessage = "We support some third party apps, native email client and copying to clipboard if your favorite app is not supported"
    EmailHelper.shared.actionSheetCancelButtonText = "Meh..."

    EmailHelper.shared.presentActionSheet(
        address: emailField.text,
        subject: subjectField.text,
        body: bodyField.text,
        presentingViewController: self,
        sender: sender)
    { (canceled, service, error) in
        if (canceled) {
            self.completionLabel.text = "User cancelled"
        }
        else if let service = service, let error = error {
            self.completionLabel.text = String(format: "Service %@ encountered error: %@", service.name, error.localizedDescription)
        }
        else if let error = error {
            self.completionLabel.text = String(format: "Encountered error: %@", error.localizedDescription)
        }
        else if let service = service {
            self.completionLabel.text = String(format: "No error, used service %@", service.name)
        }
    }

How To Install

Using Cocoapods

1) Add the following to your project Podfile:

pod 'SYEmailHelper', '~>  2.2.0'

2) Open a new terminal

3) cd to the root of your project directory

4) run pod install from the command line


Using Swift Package Manager

1) Open your project and click on the project name in project navigator 2) Under the “Project” section click your project name, then click “Package Dependecies” 3) Click the “+” button, and search for https://github.com/dvkch/SYEmailHelper.git 4) Once the package is found, be sure to select “exact version” and enter version number “2.2.0” 5) Press enter and the package will be downloaded and integrated into your Xcode Project

Import the package (both SPM & Cocoapods)

At the top of any .swift file in your project add:

import SYEmailHelper


Don't forget

You'll need to add the list of supported apps inside your Info.plist. Here are the needed items for the current list of supported apps.

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlegmail</string>
    <string>inbox-gmail</string>
    <string>ms-outlook</string>
    <string>readdle-spark</string>
</array>

Screenshots

ActionSheet

Examples

You can open both SPM and Cocoapods example projects, as well as the package itself, by opening the repo workspace.

Simply:

1) Clone this repository

2) Open the repository folder

3) Open the SYEmailHelper.xcworkspace with Xcode, which is located in the root directory of this repo


License

Use it as you like in every project you want, redistribute with mentions of my name and don't blame me if it breaks :)

-- dvkch