如何使用UIActivityItemProvider通过UIActivityViewController发送带有附件的电子邮件? [英] How do I use UIActivityItemProvider to send an email with attachment with UIActivityViewController?

查看:229
本文介绍了如何使用UIActivityItemProvider通过UIActivityViewController发送带有附件的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用UIActivityItemProvider通过电子邮件附件从我的应用程序中共享文件。我还需要填充电子邮件的主题行,并将附件的名称指定为不同于设备上存储的文件的名称。

I am trying to use UIActivityItemProvider to share a file from within my app via email attachment. I also need to populate the subject line of the email and to specify the name of the attachment to be something different than the name of the file stored on the device.

此处是我正在使用的代码。问题是电子邮件中缺少附件。

Here is the code that I'm using. The problem is that the attachment is missing from the email.

@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中为此:

And then in my viewController I do this:

      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];


推荐答案

我正在发送带有ItemProvider附件的电子邮件。其工作正常:-)

i'm sending email with attachment without ItemProvider. its working well :-)

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];

这篇关于如何使用UIActivityItemProvider通过UIActivityViewController发送带有附件的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆