Swift 4 - 使用UIActivityViewController分享到Outlook

3

我正在尝试使用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)

但是我有几个问题:

  1. 邮件主题无法工作,我进行了一些研究,发现 setValue 对于 Outlook 无效,并且第一行可以是邮件主题,但我不知道如何实现。

  2. 有没有办法除了 Outlook 之外排除所有活动?

  3. 以前我只是使用 MFMailComposeViewController 来撰写电子邮件,是否有方法可以在没有 UIActivityViewController 的情况下使用 Outlook 进行操作?

谢谢,

1个回答

5
你可以构建自己的用户界面,并按照你的喜好展示它,然后将参数转换为深度链接 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
}

用法

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

最后,请注意:

  1. 不要忘记告诉iOS你将要调用ms-outlook。(将其添加到info.plist中的LSApplicationQueriesSchemes,否则,如果您忘记了,您将在控制台中获得清晰的错误信息)

  2. 在尝试打开URL之前,也不要忘记检查应用程序是否实际存在。(canOpenURL可帮助您完成此操作)


1
是的,这个很好用,但是当我在UIActivityViewController中选择Outlook时,它只有一个撰写邮件的弹出窗口,有没有办法像那样做呢?而不是打开整个Outlook应用程序。我知道MFMailComposeViewController可以做到我期望的。 - user979331
我认为MFMailComposeViewController是为本地iOS Mail.app设计的,不是吗?此外,UIActivityViewController默认涉及任何可以与该活动交互的人。如果您想要非常具体地针对Outlook,请在接近方法中特别指定,例如调用其深度链接或使用它们的框架(如果有)。 - Mojtaba Hosseini
是的,我相信如此。 - user979331

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接