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

问题描述:

我有一个自定义文件UTI附件差不多在我的应用程序中工作时共享作为消息。它将文件作为附件嵌入消息中正确的文件类型“kitlist”,并在iPad和iPhone上接收。如果您从普通消息中点击附件,则不会发生任何事情。但是,如果点击右上角的信息按钮,然后选择附件,请点击文件并分享此文件,应用程序将显示为可能的目的地。选择它会在我的应用程序中成功打开文件。目前,选择邮件作为共享目的地不会导致附件出现在电子邮件中;我不知道这是否是相关的。我也尝试使用子类UIActivityItemProvider而不是实现协议UIActivityItemSource子类NSObject,但没有区别。对另一个iOS设备进行空中操作很好。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"; 
} 
+0

你有没有这个运气? –

+0

不,@JordanSmith,遗憾的是没有。仍然在做! – Nick

我一直与此相同的问题,现在冥思苦想了几天。我终于明白,在通讯应用程序中的附件实际上是DO的工作!他们只是不从常规观点开放。在iOS中,您需要点击右上角的i(信息)图标,然后点击“附件”标签来管理您的附件。从那里你可以选择你的附件,然后选择操作图标将文档导入你的应用程序。这似乎应该从主要的“消息”屏幕上工作,但该屏幕似乎只支持公开支持的文档类型,而不是“public.data”类型。我已经检查了你的代码和你的info.plist,在那里一切看起来都很好。我认为在plist中要记住的主要原因是,mime类型标识符应该以附件使用的实际文件扩展名结尾,您的附件使用的是。

enter image description here

+0

黑色(或深色)上的红色 - 只是不。 – LinusGeffarth