我正在尝试使用UIActivityItemProvider通过电子邮件附件从我的应用程序内共享文件。我还需要填充电子邮件的主题行,并指定附件的名称与设备上存储的文件名称不同。
这是我正在使用的代码。问题是电子邮件中缺少附件。
@interface ItemProvider:UIActivityItemProvider
@property (nonatomic, strong) NSURL *filepath;
@property (nonatomic, strong) NSString *emailBody;
@property (nonatomic, strong) NSString *emailSubject;
@end
@implementation ItemProvider
- (id)initWithPlaceholderItem:(id)placeholderItem
{
//Initializes and returns a provider object with the specified placeholder data
return [super initWithPlaceholderItem:placeholderItem];
}
- (id)item
{
//Generates and returns the actual data object
return [NSDictionary dictionary];
}
// The following are two methods in the UIActivityItemSource Protocol
// (UIActivityItemProvider conforms to this protocol) - both methods required
#pragma mark UIActivityItemSource
//- Returns the data object to be acted upon. (required)
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
if ([activityType isEqualToString:UIActivityTypeMail]) {
return @{@"body":self.emailBody, @"url":self.filepath};
}
return @{@"body":self.emailBody, @"url":self.filepath};
}
//- Returns the placeholder object for the data. (required)
//- The class of this object must match the class of the object you return from the above method
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
return @{@"body":self.emailBody, @"url":self.filepath};
}
-(NSString *) activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
return self.emailSubject;
}
@end
然后在我的viewController中我这样做:
ItemProvider *provider = [[ItemProvider alloc] initWithPlaceholderItem:@{@"body":emailBody, @"url":filePath}];
provider.emailBody = emailBody;
provider.emailSubject = info.title;
provider.filepath = filePath;
NSArray *activityItems = @[provider];
// Build a collection of custom activities (if you have any)
// NSMutableArray *customActivities = [[NSMutableArray alloc] init];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
答案 0 :(得分:33)
对于那些仍然在寻求解决方案的人来说,有一个更优雅的解决方案来定制UIActivityViewController
。为解决原始问题,附件未显示的原因是因为它应该是一个单独的UIActivityItemProvider
对象。
所以解决方案是创建两个UIActivityItemProvider
子类,一个包装'emailBody'和'emailSubject',另一个包装附件。使用UIActivityItemProvider
作为附件的好处是,您有机会在需要之前延迟处理附件,而不是在呈现UIActivityViewController
之前这样做。
实施AttachmentProvider
类以提供附件,如下所示:
@implementation AttachmentProvider : UIActivityItemProvider
- (id)item {
if ([self.activityType isEqualToString:UIActivityTypeMail]) {
/* Replace with actual URL to a file. Alternatively
* you can also return a UIImage.
*/
return [NSData dataWithContentsOfURL:dataURL];
}
return nil;
}
@end
实施EmailInfoProvider
类以提供电子邮件正文和主题类,如下所示:
@implementation EmailInfoProvider : UIActivityItemProvider
- (id)item {
return @"Your email body goes here";
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
if ([activityType isEqualToString:UIActivityTypeMail]) {
return @"Your subject goes here";
}
return nil;
}
@end
然后,您可以在viewController中创建UIActivityViewController
这两个项目,如下所示:
- (void)shareAction {
AttachmentProvider *attachment = [[AttachmentProvider alloc] init];
EmailInfoProvider *emailContent = [[EmailInfoProvider alloc] init];
// You can provider custom -(id)init methods to populate EmailInfoProvider
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[attachment, emailContent] applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}
答案 1 :(得分:7)
我发送的电子邮件附件没有ItemProvider。它运作良好: - )
NSMutableArray *selDocs = [[NSMutableArray alloc] init];
for (Document *theDoc in self.selectedDocs) {
NSURL *fileUrl = [NSURL fileURLWithPath:theDoc.filePath];
[selDocs addObject:fileUrl];
}
NSArray *postItems = [NSArray arrayWithArray:selDocs];
UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];
[avc setValue:@"Your email Subject" forKey:@"subject"];
avc.completionHandler = ^(NSString *activityType, BOOL completed){
NSLog(@"Activity Type selected: %@", activityType);
if (completed) {
NSLog(@"Selected activity was performed.");
} else {
if (activityType == NULL) {
NSLog(@"User dismissed the view controller without making a selection.");
} else {
NSLog(@"Activity was not performed.");
}
}
};
[self presentViewController:avc animated:YES completion:nil];