我尝试仅将 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)
但是我有一些问题:
主题行不起作用,我做了一些研究,显然 setValue 不适用于 Outlook,并且第一行可以作为主题行,但我不知道该怎么做。
是否可以排除除 Outlook 之外的所有事件?
之前我只是使用 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"]
)
)
最后,请注意:
不要忘记告诉 iOS 您将调用
ms-outlook
。 (将其添加到info.plist
中的LSApplicationQueriesSchemes
中,否则,如果忘记,您将在控制台中收到清晰的错误消息)另外,在尝试打开网址之前,不要忘记检查应用程序是否确实存在。 (
canOpenURL
可以为您提供帮助)
关于ios - Swift 4 - Outlook 的 UIActivityViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256655/