如何使用 UIActivityItemProvider 设置邮件主题
How to set mail subject with UIActivityItemProvider
我正在使用 UIActivityViewController
通过电子邮件共享信息。我们可以毫无问题地发送带有 body 附件的电子邮件。但是我们如何设置电子邮件的主题标题。
我注意到这个问题:How to set a mail Subject in UIActivityViewController?
公认的解决方案是使用 UIActivityItemSource
和 API activityViewController:subjectForActivityType:。但是,我们的代码不符合 UIActivityItemSource
,因为我们使用的是 UIActivityItemProvider
。
UIActivityItemSource
You can use this protocol in situations where you want to provide the data from one of your app’s existing objects instead of creating a separate UIActivityItemProvider object.
所以完整的问题是:
如果我使用 UIActivityItemProvider
而不是 UIActivityItemSource,如何设置电子邮件主题?
只需在实例化 UIActivityViewController 后添加此行:
[activityController setValue:@"Your email Subject" forKey:@"subject"];
我是这样使用的:
- (void)share {
NSArray *activityItems;
NSString *texttoshare = [NSString stringWithFormat:@"Hey bro! check this info.\n%@\n%@", self.infoBean.title, self.infoBean.desc];
UIImage *imagetoshare = imageView.image;//this is your image to share
if (imagetoshare != nil) {
activityItems = @[imagetoshare, texttoshare];
} else {
activityItems = @[texttoshare];
}
NSArray *exTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityController.excludedActivityTypes = exTypes;
[activityController setValue:@"Your email Subject" forKey:@"subject"];
[self presentViewController:activityController animated:YES completion:nil];
}
定义您的自定义项目提供者:
@interface CustomProvider : UIActivityItemProvider
@end
添加到您的实施中:
@implementation CustomProvider
// Some other code ... -(id)item and etc.
- (NSString *) activityViewController:(UIActivityViewController *)activityViewController
subjectForActivityType:(NSString *)activityType
{
return @"A dummy Title";
}
@end
注意 UIActivityItemProvider
将自动符合 UIactivityItemSource
协议。区别在于,您不必为 UIactivityItemSource
协议实现那些 @required
API。
UIActivityItemProvider
实现了 UIActivityItemSource
协议。它就在 header.
中
@interface UIActivityItemProvider : NSOperation <UIActivityItemSource>
因此您可以简单地使用方法 - (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
来 return 您的 UIActivityItemProvider
子类中的主题。
我正在使用 UIActivityViewController
通过电子邮件共享信息。我们可以毫无问题地发送带有 body 附件的电子邮件。但是我们如何设置电子邮件的主题标题。
我注意到这个问题:How to set a mail Subject in UIActivityViewController?
公认的解决方案是使用 UIActivityItemSource
和 API activityViewController:subjectForActivityType:。但是,我们的代码不符合 UIActivityItemSource
,因为我们使用的是 UIActivityItemProvider
。
UIActivityItemSource
You can use this protocol in situations where you want to provide the data from one of your app’s existing objects instead of creating a separate UIActivityItemProvider object.
所以完整的问题是:
如果我使用 UIActivityItemProvider
而不是 UIActivityItemSource,如何设置电子邮件主题?
只需在实例化 UIActivityViewController 后添加此行:
[activityController setValue:@"Your email Subject" forKey:@"subject"];
我是这样使用的:
- (void)share {
NSArray *activityItems;
NSString *texttoshare = [NSString stringWithFormat:@"Hey bro! check this info.\n%@\n%@", self.infoBean.title, self.infoBean.desc];
UIImage *imagetoshare = imageView.image;//this is your image to share
if (imagetoshare != nil) {
activityItems = @[imagetoshare, texttoshare];
} else {
activityItems = @[texttoshare];
}
NSArray *exTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityController.excludedActivityTypes = exTypes;
[activityController setValue:@"Your email Subject" forKey:@"subject"];
[self presentViewController:activityController animated:YES completion:nil];
}
定义您的自定义项目提供者:
@interface CustomProvider : UIActivityItemProvider
@end
添加到您的实施中:
@implementation CustomProvider
// Some other code ... -(id)item and etc.
- (NSString *) activityViewController:(UIActivityViewController *)activityViewController
subjectForActivityType:(NSString *)activityType
{
return @"A dummy Title";
}
@end
注意 UIActivityItemProvider
将自动符合 UIactivityItemSource
协议。区别在于,您不必为 UIactivityItemSource
协议实现那些 @required
API。
UIActivityItemProvider
实现了 UIActivityItemSource
协议。它就在 header.
@interface UIActivityItemProvider : NSOperation <UIActivityItemSource>
因此您可以简单地使用方法 - (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
来 return 您的 UIActivityItemProvider
子类中的主题。