简体   繁体   中英

Swift 4 - UIActivityViewController for Outlook

I am trying to use UIActivityViewController for outlook only...I was able to get my UIActivityViewController working like so:

//Define HTML String

        var htmlString = ""

        //Add the headings to HTML String table

        htmlString += "<table border = '1' cellspacing='0' align = 'center'><tr><th>Job #</th><th>Task</th><th>Date</th></tr>"

        //For each item in punch list data array

        for i in 0..<punchListData.count {

            //If the item is selected

            if punchListData[i].cellSelected {

                //Add data to the HTML String

                htmlString += "<tr><td>" + jobList[i % jobList.count] + "</td><td align = 'center'>" + taskData[i / jobList.count] + "</td><td>" + (punchListData[i].stringData)! + "</td></tr>"
            }
        }

        //Close the HTML table in the HTML String

        htmlString += "</table><h5>Please contact me if you have any questions <br /> Thank you.</h5>"

        let activityViewController = UIActivityViewController(activityItems : [htmlString], applicationActivities: nil)

        activityViewController.setValue("Schedule for Community", forKey: "Subject")

        activityViewController.popoverPresentationController?.barButtonItem = self.shareButton

        self.present(activityViewController, animated: true, completion: nil)

But I have a few issues:

  1. The subject line is not working, I did some research and apparently setValue will not work for Outlook and that the first line can be the subject line, but I have no idea how to do that.

  2. Is there away to exclude all activities except for Outlook?

  3. Previously I was just using MFMailComposeViewController to compose an email, is there away of doing this for Outlook? Without UIActivityViewController?

Thanks,

You can build your own UI and present it as you like, then convert arguments to a deeplink URL and pass it to the outlook app:

// MARK: - Errors
enum MailComposeError: Error {
    case emptySubject
    case emptyBody
    case unexpectedError
}

// MARK: - Helper function
func outlookDeepLink(subject: String, body: String, recipients: [String]) throws -> URL {
    guard !subject.isEmpty else { throw MailComposeError.emptySubject }
    guard !body.isEmpty else { throw MailComposeError.emptyBody }
    
    let emailTo = recipients.joined(separator: ";")
    
    var components = URLComponents()
    components.scheme = "ms-outlook"
    components.host = "compose"
    components.queryItems = [
        URLQueryItem(name: "to", value: emailTo),
        URLQueryItem(name: "subject", value: subject),
        URLQueryItem(name: "body", value: body),
    ]
    
    guard let deepURL = components.url else { throw MailComposeError.unexpectedError }
    return deepURL
}

Usage

try! UIApplication.shared.open(
    outlookDeepLink(
        subject: "subject",
        body: "body",
        recipients: ["example@email.com", "example2@email.com"]
    )
)

At last, Note that:

  1. Don't forget to tell iOS you are going to call ms-outlook . (Add it to LSApplicationQueriesSchemes in info.plist , Otherwise, You will get an clear error message in console if you forget it)

  2. Also don't forget to check if the app actually exists before trying to open the url. ( canOpenURL is here to help)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM