简体   繁体   English

Swift 4 - 用于 Outlook 的 UIActivityViewController

[英]Swift 4 - UIActivityViewController for Outlook

I am trying to use UIActivityViewController for outlook only...I was able to get my UIActivityViewController working like so:我试图将 UIActivityViewController 仅用于 Outlook ......我能够让我的 UIActivityViewController 像这样工作:

//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.主题行不起作用,我做了一些研究,显然 setValue 不适用于 Outlook,第一行可以是主题行,但我不知道该怎么做。

  2. Is there away to exclude all activities except for Outlook?是否可以排除除 Outlook 之外的所有活动?

  3. Previously I was just using MFMailComposeViewController to compose an email, is there away of doing this for Outlook?以前我只是使用 MFMailComposeViewController 来撰写电子邮件,是否可以为 Outlook 执行此操作? Without UIActivityViewController?没有 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:您可以构建自己的 UI 并根据需要呈现它,然后将参数转换为深层链接 URL 并将其传递给 Outlook 应用程序:

// 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 .不要忘记告诉 iOS 你要调用ms-outlook (Add it to LSApplicationQueriesSchemes in info.plist , Otherwise, You will get an clear error message in console if you forget it) (将其添加到info.plist LSApplicationQueriesSchemes ,否则,如果您忘记它,您将在控制台中收到明确的错误消息)

  2. Also don't forget to check if the app actually exists before trying to open the url.另外不要忘记在尝试打开 url 之前检查该应用程序是否确实存在。 ( canOpenURL is here to help) canOpenURL在这里提供帮助)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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