我在分享为消息时,在我的应用程序中有一个自定义文件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";
}