第一篇文章,所以我将尝试直接并尽可能详细...我使用UIActivityViewController并继承UIActivityItemProvider
以根据共享方法以不同方式处理共享文本选择(这个线程有很多帮助:How to know which icon is clicked in UIActivityViewController before activityController setCompletionHandleris called?)。
我想我已经找到了传入源数据然后根据所选的itemForActivityType进行处理的机制,但是对于电子邮件共享,我想预先填充标题(主题,正文等)以及附加文件。我知道如何使用独立版MFMailComposeViewController
执行此操作,但我不太清楚如何使用ActivityViewController
执行此操作。在这里:UIActivityViewController - Email and Twitter sharing,提到在初始化器上使用值/键对,如:
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
[activityViewController setValue:@"My Subject Text" forKey:@"subject"];
但是设置收件人和邮件正文的其他标准键是什么?
附加文档是我的另一个重要问题。我知道如何创建一个文档并将其保存在文件系统上并附加它 - 再次使用MFMailComposeViewController - 但我希望使用UIActivityViewController获得相同的行为。
(对不起... ...)
答案 0 :(得分:0)
设置应使用的主题
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
对于您的数据(文本和图像),您可以通过activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType{
传递它们,这将返回您的数据的UTI,以便服务可以更好地处理数据。您使用的密钥并不重要,因为UTI将有助于识别项目。 Apple Doc:
提供UTI允许服务以适当的方式处理特定的数据类型,例如格式化图像以在线显示的电子邮件服务。 More info here
答案 1 :(得分:0)
我不认为您可以在不创建自己的UIActivity子类(YourActivity)的情况下直接访问邮件控制器。
在“YourActivity”中设置MFMailComposeViewController,它将像在主代码中一样运行。我是这样做的:
在YourActivity.h中:
让自己成为邮件控制器委托并为邮件视图控制器和所选视图控制器设置方法范围的变量:
@interface YourActivity : UIActivity <MFMailComposeViewControllerDelegate>
{
MFMailComposeViewController *mailController;
UIViewController *activityViewController;
}
在YourActivity.m中:
(可选)我建议您尽早检查邮件服务的可用性。 (这将阻止向用户提供无法完成的选项):
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
// If mail is unavailable, can't perform activity
if (![MFMailComposeViewController canSendMail]) {
return NO;
}
for (id item in activityItems) {
// whatever other checks you want to do
return YES;
}
return NO;
}
在YourActivity -prepareWithActivityItems:方法中设置MFMailComposeViewController:
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
// See if we can send mail (shouldn't happen if we checked already in -canPerformActivityWithItems)
if (![MFMailComposeViewController canSendMail]) {
UIAlertController *mailAlertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Mail Unavailable", @"mail unavailable")
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"cancel")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
[self activityDidFinish:NO];
}];
[mailAlertController addAction:cancel];
// Set the alert as the view to return
activityViewController = mailAlertController;
}
// Create a mail view controller
mailController = [[MFMailComposeViewController alloc] init];
// Set Delegate
[mailController setMailComposeDelegate:self];
// Set mail controller as the view to return
activityViewController = mailController;
// Paste the rest of your MFMailComposeViewController code here
}
在YourActivity -activityViewController方法中,返回选定的视图控制器:
- (UIViewController *)activityViewController
{
return activityViewController;
}
请记住实现邮件完成处理程序,至少要关闭编写器视图:
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// Send any messages, if desired, to the controller before dismissing
NSString *message = nil;
NSString *errorMessage = nil;
if (result == MFMailComposeResultFailed) {
message = NSLocalizedString(@"Unable to send email", @"Unable to send email");
}
if (error) {
errorMessage = [message stringByAppendingString:[NSString stringWithFormat:NSLocalizedString(@"Error:\n%@", @"error:\n%@"), [error localizedDescription]]];
}
// Send mail status alert message, if needed
if (message) {
UIAlertController *mailAlert = [UIAlertController alertControllerWithTitle:message
message:errorMessage
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
// Dismiss the mail controller
[controller dismissViewControllerAnimated:true completion:^{}];
[controller release];
mailController = nil;
}];
[mailAlert addAction:cancel];
[controller presentViewController:mailAlert animated:YES completion:^{
//
}];
}
else {
// Dismiss the mail controller
[controller dismissViewControllerAnimated:true completion:^{}];
[controller release];
mailController = nil;
}
}