In my app, I need to send some custom data files from one device to another, and I am trying to do this with Mail, iMessage/Message and Airdrop.
This works fine with Mail and Airdrop but with iMessage and it goes just fine, but on the receiving end, I am not able to open the files. It's just not allowing me to do anything with it.
Any ideas??
This is what my Document Type looks like:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFile</key>
<string>abc.png</string>
<key>CFBundleTypeName</key>
<string>ABC Custom Data type</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>Handler Rank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.company.abc.wd</string>
</array>
</dict>
</array>
This is how i am sending the data:
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
[dict setObject:currentDataSet forKey:@"actualData"];
NSData * meetingData = [NSKeyedArchiver archivedDataWithRootObject:dict];
Meeting * dataItem = [[Meeting alloc]initWithData:meetingData
type:@"com.abc.xyz.wd" subject:@"Meeting"
previewImage:[UIImage imageNamed:@"appIcon.png"]];
UIActivityViewController * activityController =
[[UIActivityViewController alloc]initWithActivityItems:@[dataItem]
applicationActivities:nil];
activityController.excludedActivityTypes =
@[UIActivityTypePostToTwitter, UIActivityTypePostToWeibo];
[self presentViewController:activityController animated:YES completion:nil];
The value in your Info.plist for the
LSItemContentTypes
key should equal what is declared by your Meeting object.Presumably your Meeting object adheres to the
UIActivityItemSource
protocol. Make sure the value you return (from the delegate methodactivityViewController:dataTypeIdentifierForActivityType:
) matches the value you have declared as readable in yourInfo.plist
.This answer is correct in that the custom document can be opened from Messages if it conforms to
public.text
. The drawback to this solution is that the document is previewed as raw text, which might not be the desired result.Documents conforming to
public.data
can be opened from the Messages app without being previewed as raw text by creating a Quick Look Preview Extension. There isn't much documentation about how to build a Quick Look Preview Extension, but it's pretty straightforward:In Xcode, choose,
File
>New
>Target
.Choose Quick Look Preview Extension, give your extension a name, and click Finish.
In the
info.plist
for the newly created extension, add a new item underNSExtension
>NSExtensionAttributes
>QLSupportedContentTypes
, and set the value for this item to your app's custom document type. For example:Use
MainInterface.storyboard
andPreviewViewController
to define the layout of your custom Quick Look preview. More specifically, read data from the URL provided in thepreparePreviewOfFile
function and populate the ViewController accordingly. A brief example (in Swift 4):Some pitfalls I ran into when creating my extension:
The exported UTI identifier had to be all lower case. When some of the characters were upper case, the Quick Look preview was never shown, even though I used the same capitalization in my Quick Look Preview Extension.
Quick Look Preview Extensions are not allowed to link against dynamic libraries. If a dynamic library is linked, the Quick Look preview will not load.
The Quick Look ViewController is not allowed to have any buttons. If it contains a button, the Quick Look preview will not load.
Additional resources:
Building Great Document-based Apps in iOS 11 (WWDC 2017 Video)
Quick Look Previews from the Ground Up (WWDC 2018 Video)
QuickLook Documentation
I came across this post while searching for a similar solution. I was able to email custom files from my app and open it in email or use it with AirDrop. If I sent it via iMessage it even showed up with my custom icon, but when I tapped it in iMessage nothing happened.
Be aware that you need something like the following in your plist file (from How do I associate file types with an iPhone application?)
NOTE: I had something very similar for my app BUT in the UTTypeConformsTo I only had public.data since my files are zipped data files.
I found that by adding public.text as a second item in the array it would be actionable in iMessage. On a further note, if I added public.plain-text as a third item my file ended up with a Pages icon instead of my icon (so I removed it)
I hope this helps someone. It's taken me hours to get to the bottom of it.