如何使用UIActivityItemProvider添加多个电子邮件附件

时间:2017-07-21 15:31:53

标签: objective-c attachment uiactivityviewcontroller uiactivityitemprovider

我正在使用iOS应用程序,我希望使用UIActivityItemProvider将 MULTIPLE 附件添加到电子邮件中。我想使用UIActivityItemProvider来实现它,因为在向用户显示UIActivtyViewController之前,我不想承担处理记录的开销。如果我使用NSData或NSURL对象将一个图像传入" url"返回对象的元素,然后内联图像显示正常。如果我传入这些对象的数组,那么什么都没有显示出来。我相信,如果我在初始化NSActivityViewController时使用ActivityItems参数,则传递数组将会起作用,但同样,我不想这样做,因为我想利用UIActivityItemProvider提供的延迟处理。以下是我的代码

@implementation NoteRecordActivityProvider

- (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
    NSData *imageFile = [[NSData alloc]init];
    NSString *imageFileName;
    NSURL *url;
    NSString* exportPath;
    NSMutableArray* imageArray = [[NSMutableArray alloc]initWithCapacity:0];
    NSInteger photoCount = self.noteRecord.photoCount;
    for (NSInteger i = 0; i < photoCount; i+=1)
    {
        //Add File Attachment
        PhotoObject *po = (PhotoObject*)[self.noteRecord photoObjects:i];
        NSString *photoGUID = [(PhotoObject*)[self.noteRecord photoObjects:i]GUID];
        imageFile = ImageDataReturningMethodHere;
        imageFileName = [[NSArray arrayWithObjects:@"Image", [NSString stringWithFormat:@"%ld", (long)i], @".png", nil] componentsJoinedByString:@""];
        exportPath = [[FileSystemProvider exportPath] stringByAppendingPathComponent:imageFileName];
        [imageFile writeToFile:exportPath atomically:YES];
        url = [NSURL fileURLWithPath:exportPath];
        [imageArray addObject:url];
    }
    if ([self.activityType isEqualToString:UIActivityTypeMail])
        return imageArray;
    else
        return nil;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return @{@"body":@"", @"url":[[NSURL alloc]init]};
}

-(NSString *) activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
    return [NSString stringWithFormat:@"Attached Record:  %@", self.noteRecord.title];
}

@end

1 个答案:

答案 0 :(得分:0)

我确实找到了这个问题的答案。首先,我创建了一个PhotoAttachmentActivityProvider,它具有源文档的属性,其中包含我要附加的照片,以及该文档中附件的索引。我在这里粘贴我的代码,该代码使用名为NoteRecord的自定义文档:

@interface EMailPhotoAttachmentItemProvider : UIActivityItemProvider
@property (nonatomic, readwrite) NSInteger photoIndex;
@property (nonatomic, strong) NoteRecord* noteRecord;
@end

然后当我展示UIActivityViewController时,我为每个附件添加了一个这些自定义UIActivityItemProvider对象:

for (NSInteger i = 0; i < self.noteRecord.photoCount;  i++)
{
    EMailPhotoAttachmentItemProvider* photoProvider =     [[EMailPhotoAttachmentItemProvider alloc]initWithPlaceholderItem:@{@"body":textToShare, @"url":url}];
    photoProvider.photoIndex = i;
    photoProvider.noteRecord = self.noteRecord;
    [activityProviders addObject:photoProvider];
}

//Initialize the ActivityViewController
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityProviders applicationActivities:applicationActivities];

然后在自定义UIActivityItemProvider中,我检查我是否正在处理EMAIL,然后使用提供的文档和图像索引为图像创建URL:

#import "EMailPhotoAttachmentItemProvider.h"
#import "MiscUtilities.h"
#import "FileSystemProvider.h"

@implementation EMailPhotoAttachmentItemProvider

- (id)initWithPlaceholderItem:(id)placeholderItem
{
    //Initializes and returns a provider object with the specified placeholder data
    return [super initWithPlaceholderItem:placeholderItem];
}

- (id)item
    {    
    if ([self.activityType isEqualToString:UIActivityTypeMail])
    {
        // Code here gets the image file from the NoteRecord at the PhotoIndex provided to  
        // the UIActivityItemProvider at the imageIndex, creates a URL for that image and returns it here.  
        // Your implementation will vary

        PhotoObject *po = (PhotoObject*)[self.noteRecord photoObjects:self.photoIndex];
        NSString *photoGUID = [(PhotoObject*)[self.noteRecord photoObjects:self.photoIndex]GUID];
        NSData *imageFile = [[[MiscUtilities getApplicationDelegate]imageProvider]imageDataWithCaptionFromGUID:photoGUID caption:po.caption maxResolution:600];
        NSString *imageFileName = [[NSArray arrayWithObjects:@"Image", [NSString stringWithFormat:@"%ld", (long)self.photoIndex], @".png", nil] componentsJoinedByString:@""];
        imageFileName = [[FileSystemProvider exportPath] stringByAppendingPathComponent:imageFileName];
        [imageFile writeToFile:imageFileName atomically:YES];
        NSURL *url = [NSURL fileURLWithPath:imageFileName];
        return url;
    }
    else
    {
        return nil;
    };

}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    NSString* defaultImagePath = [[FileSystemProvider imagePath]stringByAppendingPathComponent:@"default.png"];
    NSURL *url = [[NSURL alloc]initWithString:defaultImagePath];
    return @{@"body":@"", @"url":url};
}
@end