iOS自定义附件无法直接从消息中打开

时间:2017-02-19 11:35:13

标签: ios objective-c uiactivityitemprovider

我在分享为消息时,在我的应用程序中有一个自定义文件UTI附件几乎。它将文件作为附件嵌入到消息中的正确文件类型“kitlist”,并在iPad和iPhone上接收。如果您从正常消息显示点击附件没有任何反应。但是,如果您点击右上角的信息按钮,然后选择附件,点击该文件并共享,该应用程序将显示为可能的目的地。选择它将在我的应用程序中成功打开文件。目前,选择Mail作为共享目的地不会导致附件出现在电子邮件中;我不知道这是否相关。我也尝试过使用子类UIActivityItemProvider,而不是实现协议UIActivityItemSource子类NSObject,但没有区别。对另一台iOS设备进行空间处理可以正常工作。

这是我的plist的相关片段:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconFiles</key>
        <array/>
        <key>CFBundleTypeName</key>
        <string>KitList Transfer File Type</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>co.SM.KitListFile.kitlist</string>
        </array>
    </dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeDescription</key>
        <string>KitList Transfer File</string>
        <key>UTTypeIdentifier</key>
        <string>co.SM.KitListFile.kitlist</string>
        <key>UTTypeSize320IconFile</key>
        <string>BergenBig</string>
        <key>UTTypeSize64IconFile</key>
        <string>Bergen</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <string>kitlist</string>
        </dict>
    </dict>
</array>

制作附件的代码是:

@interface SMCustomAttachmentTransfer()
@property (nonatomic, strong) NSMutableArray *images;

@end

@implementation SMCustomAttachmentTransfer

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    self.images =  [NSMutableArray array];
    [aCoder encodeObject:self.list forKey:@"list"];
    [self walkTheList:[self.list valueForKey:keySubItems] withCoder:aCoder];
    [aCoder encodeObject:self.images forKey:@"images"];
}

- (void)walkTheList:(NSDictionary *)items withCoder:(NSCoder *)aCoder
{
    for (NSDictionary *item in items)
    {
        if ( [item valueForKey:keyDetailImage] )
        {
            [self.images addObject:[item valueForKey:keyDetailImage]];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[item valueForKey:keyDetailImage]];
            [aCoder encodeObject:[NSData dataWithContentsOfFile:getImagePath] forKey:[item valueForKey:keyDetailImage]];
        }
        if ( [item valueForKey:keySubItems] )
        {
            [self walkTheList:[item valueForKey:keySubItems] withCoder:aCoder];
        }
    }
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self)
    {
        self.list = [aDecoder decodeObjectForKey:@"list"];
        NSArray *images = [aDecoder decodeObjectForKey:@"images"];
        for (NSString *image in images)
        {
            NSData *pngData = [aDecoder decodeObjectForKey:image];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsPath = [paths objectAtIndex:0];
            [pngData writeToFile:[documentsPath stringByAppendingPathComponent:image] atomically:YES];
        }
    }
    return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    // must be data placeholder for attachment to be embedded in a message.
    return [NSData data];
}

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
{
    return [self.list valueForKey:keyName];
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType
{
   return [NSKeyedArchiver archivedDataWithRootObject:self];
}

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(NSString *)activityType
{
    return @"co.SM.KitListFile.kitlist";
}

1 个答案:

答案 0 :(得分:2)

我已经和这个相同的问题摔跤了几天了。我终于发现消息应用程序中的附件实际上 DO 正常工作!他们只是不从常规视图打开。在iOS中,您必须单击右上角的i(信息)图标,然后单击“附件”选项卡以管理附件。从那里,您可以选择附件,然后选择操作图标将文档导入您的应用程序。看起来这应该在主要的“消息”屏幕上工作,但该屏幕似乎只支持公共支持的文档类型,而不是“public.data”类型。我已经检查了你的代码和你的info.plist,一切似乎很好。我认为在plist中要记住的主要事情是mime类型标识符应该以你的附件使用的实际文件扩展名结束,这是你的。

enter image description here