iOS 自定义附件无法直接从消息中打开
iOS Custom attachment not opening directly from Messages
我有一个自定义文件 UTI 附件 几乎 在我的应用程序中作为消息共享时工作。它将文件作为附件嵌入到邮件中,文件类型为 'kitlist',并且在 iPad 和 iPhone 上都可以收到。如果您点击正常消息显示中的附件,则不会发生任何事情。但是,如果您点击右上角的信息按钮,然后选择附件,点击文件并共享,该应用程序将显示为可能的目的地。选择它将在我的应用程序中成功打开文件。目前,选择邮件作为共享目的地不会导致附件出现在电子邮件中;我不知道这是不是 relevant.I我也尝试过使用子类 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";
}
几天来我一直在努力解决这个相同的问题。我终于发现消息传递应用程序中的附件实际上 DO 有效!他们只是不从常规视图中打开。在 iOS 中,您必须单击右上角的 i(信息)图标,然后单击 "Attachments" 选项卡来管理您的附件。从那里您可以 select 您的附件,然后 select 将文档导入您的应用程序的操作图标。这似乎应该在主 "messages" 屏幕上工作,但该屏幕似乎只支持公开支持的文档类型,而不支持 "public.data" 类型。我检查了你的代码和你的 info.plist ,一切似乎都很好。我认为在 plist 中要牢记的主要事情是 mime 类型标识符应该以附件使用的实际文件扩展名结尾,您的也是如此。
我有一个自定义文件 UTI 附件 几乎 在我的应用程序中作为消息共享时工作。它将文件作为附件嵌入到邮件中,文件类型为 'kitlist',并且在 iPad 和 iPhone 上都可以收到。如果您点击正常消息显示中的附件,则不会发生任何事情。但是,如果您点击右上角的信息按钮,然后选择附件,点击文件并共享,该应用程序将显示为可能的目的地。选择它将在我的应用程序中成功打开文件。目前,选择邮件作为共享目的地不会导致附件出现在电子邮件中;我不知道这是不是 relevant.I我也尝试过使用子类 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";
}
几天来我一直在努力解决这个相同的问题。我终于发现消息传递应用程序中的附件实际上 DO 有效!他们只是不从常规视图中打开。在 iOS 中,您必须单击右上角的 i(信息)图标,然后单击 "Attachments" 选项卡来管理您的附件。从那里您可以 select 您的附件,然后 select 将文档导入您的应用程序的操作图标。这似乎应该在主 "messages" 屏幕上工作,但该屏幕似乎只支持公开支持的文档类型,而不支持 "public.data" 类型。我检查了你的代码和你的 info.plist ,一切似乎都很好。我认为在 plist 中要牢记的主要事情是 mime 类型标识符应该以附件使用的实际文件扩展名结尾,您的也是如此。