ios 如何在 UIActivityViewController 中设置邮件主题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17020288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a mail Subject in UIActivityViewController?
提问by Femina
I want to set subject for email sharing in UIActivityViewController
and also want to share in Twitter. I know in Twitter if we want to share — we need compress text to 140 chars. I checked many SO solutions, but nothing is working.
我想设置电子邮件共享的主题UIActivityViewController
,也想在 Twitter 中共享。我在 Twitter 上知道如果我们想分享——我们需要将文本压缩为 140 个字符。我检查了许多 SO 解决方案,但没有任何效果。
Is this issue fixed in latest iOS releases? Any other "working solutions"?
这个问题在最新的 iOS 版本中修复了吗?任何其他“工作解决方案”?
回答by emreoktem
Check below code for the email for setting up your email subject:
检查以下电子邮件代码以设置您的电子邮件主题:
UIActivityViewController* avc = [[UIActivityViewController alloc] initWithActivityItems:@[@"Your String to share"]
applicationActivities:nil];
[avc setValue:@"Your email Subject" forKey:@"subject"];
avc.completionHandler = ^(NSString *activityType, BOOL completed) {
// ...
};
Here the line
这里的线
[avc setValue:@"Your email Subject" forKey:@"subject"];
[avc setValue:@"Your email Subject" forKey:@"subject"];
Makes the subject as "Your email Subject" if user picks email option in the UIActivityViewController.
如果用户在 UIActivityViewController 中选择电子邮件选项,则将主题设为“您的电子邮件主题”。
I hope it helps...
我希望它有帮助...
回答by Tim Camber
It seems as though emreoktem's solution—sending setValue:forKey:
to the UIActivityViewController
—is undocumented.
似乎 emreoktem 的解决方案 - 发送setValue:forKey:
到UIActivityViewController
- 没有记录。
On iOS 7 and later, you can implement the activityViewController:subjectForActivityType:
method in an object conforming to the UIActivityItemSource
protocol to do this in a way that is documented.
在 iOS 7 及更高版本上,您可以activityViewController:subjectForActivityType:
在符合UIActivityItemSource
协议的对象中实现该方法,以便以记录在案的方式执行此操作。
回答by biomiker
Here's a concrete solution for Swift 3.0+based on the accepted answer. Note that, like the accepted answer, this is known to work only on the iOS Mail app and not necessarily other apps.
这是基于公认答案的Swift 3.0+的具体解决方案。请注意,与接受的答案一样,已知这仅适用于 iOS 邮件应用程序,不一定适用于其他应用程序。
Implementation:
执行:
class MessageWithSubject: NSObject, UIActivityItemSource {
let subject:String
let message:String
init(subject: String, message: String) {
self.subject = subject
self.message = message
super.init()
}
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return message
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
return message
}
func activityViewController(_ activityViewController: UIActivityViewController,
subjectForActivityType activityType: UIActivityType?) -> String {
return subject
}
}
Usage:
用法:
Here's an example of usage. Note that it works well to use this as the first item in the activityItems array, and include any additional items to follow:
这是一个使用示例。请注意,将它用作 activityItems 数组中的第一项效果很好,并包含要遵循的任何其他项:
let message = MessageWithSubject(subject: "Here is the subject", message: "An introductory message")
let itemsToShare:[Any] = [ message, image, url, etc ]
let controller = UIActivityViewController(activityItems: itemsToShare, applicationActivities: nil)
回答by PAC
For Swift 2.0+ & ios 8.0+
适用于 Swift 2.0+ 和 ios 8.0+
let title = "Title of the post"
let content = "Content of the post"
let objectsToShare = [title, content]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.setValue(title, forKey: "Subject")
self.presentViewController(activityVC, animated: true, completion: nil)