ios - Swift 4 - Outlook 的 UIActivityViewController

标签 ios swift

我尝试仅将 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 来撰写电子邮件,有没有办法在 Outlook 中执行此操作?没有 UIActivityViewController?

谢谢

最佳答案

您可以构建自己的 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
}

用法

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

最后,请注意:

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

  2. 另外,在尝试打开网址之前,不要忘记检查应用程序是否确实存在。 (canOpenURL 可以为您提供帮助)

关于ios - Swift 4 - Outlook 的 UIActivityViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256655/

相关文章:

swift - 为什么打印对象的实例会使其陷入无限循环,并因错误 EXC_BAD_ACCESS 而崩溃?

ios - Objective-C (iOS),使用 CIFilter 调整图像大小会产生无效输出 (UIImage)

ios - onFormat string For NSPredicate

android - 有没有办法在cordova运行应用程序的设备功能上启动谷歌设备检查

ios - Firebase:我应该如何构建我的数据库?喜欢 Offerup 和 Letgo

ios - 使用 contains 检查 NSManagedObject Array 是否具有特定键的值

ios - 迭代字典数组的数组

json - Three20 和 Facebook Graph API 问题

ios - 当我从详细 View 返回时图像数组加倍

ios - 如何快速履行零 promise