open text files with my ios app - ios

I am trying to register my app as being able to open text files. Based on some digging, below is what I tried and while it compiled, it didnt work. When I go into dropbox and try to launch a txt file, I get 'No installed apps support opening this file'. What do I need to do to make my app show up for any txt file?
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>Text File</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleTypeExtensions</key>
<array>
<string>txt</string>
<string>TXT</string>
</array>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.txt</string>
</array>
</dict>
</array>

My app imports TXT files, too.
Change your Info.plist:
Change public.txt in the listing above to com.myapp.txt (or whatever you want)
After the last </array> of your listing above add:
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.text</string>
</array>
<key>UTTypeDescription</key>
<string>Plain Text Document</string>
<key>UTTypeIdentifier</key>
<string>com.myapp.txt</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>txt</string>
<key>public.mime-type</key>
<string>text/plain</string>
</dict>
</dict>
</array>
You may replace com.myapp.txt with whatever identifier you want.
I hope this helps.

Answer updated for iOS 11 and Xcode 9
If the document type you are adding is not a custom document type, you don't have to "register your app" because the standard types are already know by iOS (this is the case of text files).
Just select the target in your project and enter the text file type in the Document Types section:
For more info, please see the "Adding A Document Type" section of this Apple's document: https://developer.apple.com/library/content/qa/qa1587/_index.html

Related

Registering new extension that opens in own iOS-App

I created my own filetype "dmate". Assume I exported a .dmate file to iPhones Files. When clicking on that file, it is looking like this - the JSON-Content is displayed:
Now I can click on the share button and open the file in my App DietMate - which works fine:
Problem: I do not want the user to see JSON text but want the Files-App and WhatsApp and so on to open the file immediately in my App without doing a preview.
Here is the relevant part of my plist.info
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>Food</string>
<key>CFBundleTypeIconFiles</key>
<array>
<string>dmate.ft.png</string>
<string>dmate.ft.png</string>
</array>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>de.example.app.food.dmate</string>
</array>
</dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.json</string>
</array>
<key>UTTypeDescription</key>
<string>Food</string>
<key>UTTypeIdentifier</key>
<string>de.example.app.food.dmate</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<string>dmate</string>
<key>public.mime-type</key>
<string>food/x-dmate</string>
</dict>
</dict>
</array>
Does anyone know, how to open my custom filetype in my own app?
If you want your app to launch right away after tapping the file in the Files app, try adding this key-value pair to your Info.plist:
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
Another thing you could try is to set "Conforms To" to public.data instead of public.json. This means that the system will no longer assume that your data is JSON, and hopefully stop displaying it that way. It might have some implications on what sharing channels are supported, and I haven’t yet managed to fully wrap my head around this.

Adding file icon to custom file type in Xcode

I'm trying to add an icon (a PNG named "FileIcon") to a custom file type in Xcode 13, but can't seem to actually add it via the interface in Xcode:
Any other way to do this?
Per Apple's article Setting Up a Document Browser App, there is a snippet in there for the plist:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>Text</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.data</string>
</array>
</dict>
</array>
Is this set up correctly in your plist?

How do I pick a .pgw file in the iOS document picker?

I'm developing an iOS app that needs to read .pgw files (https://en.wikipedia.org/wiki/World_file) from the local device storage. I have a sample file on my device that I'm trying to open from within the app. When I open the document picker, I can see the file but it's greyed out and I can't select it. In that screenshot, malaysia-google-earth.pgw is the file I'm trying to open.
If I change the file extension of that file to .txt, it's no longer greyed out and I can select it. So I know it's a problem with the file extension.
I've tried to add .pgw to the supported document types list for the app, but since there is no Uniform Type Identifier for this extension, I'm not sure if I'm adding it correctly. My Info.plist file includes these keys:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>PNG World File</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.pgw</string>
</array>
</dict>
</array>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeDescription</key>
<string>PNG World File</string>
<key>UTTypeIconFiles</key>
<array/>
<key>UTTypeIdentifier</key>
<string>public.pgw</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>pgw</string>
</array>
</dict>
</dict>
</array>
<true/>
I am using iOS version 13.6, and this is a React Native app using react-native-document-picker.

Create file with custom document type

I have defined a custom document type containing binary data for my app, with identifier "com.mycompany.gcp", using an Exported UTI. I am using this document type to exchange data with macOS and iOS versions of my app. After much trial and error, it is working reasonably well. However, I feel like I am missing a critical step.
When I export a file from my app, I create the file using FileManager.createFile(atPath:, contents:, attributes:). I create the file with my gcp extension, however I do not know how to specify the identifier type, specifically to set it to "com.mycompany.gcp". When I export the file, if I inspect it with mdls, kMDItemContentType = "dyn.ah62d4rv4ge80s25u", which I assume refers to a dynamic identifier, rather than my identifier.
My questions are: 1) is there a way to set the identifier when creating the file? I have tried the NSURL methods to set the identifier without success. 2) does this matter?
I feel like the answer the question 2 is yes. With iOS, if I email or airdrop the file, I can open it with my app. However, with iMessage, no action occurs when I tap on a sent file.
Here are the relevant keys from the Info.plist for the iOS version of my app:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>GCP File</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.mycompany.gcp</string>
</array>
</dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeDescription</key>
<string>GCP File</string>
<key>UTTypeIdentifier</key>
<string>com.mycompany.gcp</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>gcp</string>
</array>
</dict>
</dict>
</array>

Trying to define GPX document type in Xcode 6.4

I'm trying to define a GPX document type so that I can open gpx file types from other applications. I've followed Apple's Technical Q&A on this topic.
I've also tried solutions that I found that edited the plist file directly. None of them have worked. I still don't see my application in the "Open In" menu when I try to open a gpx file. I'm pretty sure I'm missing something here. I just don't have any idea what it is. Please help.
<key>UTImportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.xml</string>
</array>
<key>UTTypeDescription</key>
<string>GPS Exchange Format (GPX)</string>
<key>UTTypeIdentifier</key>
<string>com.topografix.gpx</string>
<key>UTTypeReferenceURL</key>
<string>http://www.topografix.com/GPX/1/1</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>gpx</string>
</array>
<key>public.mime-type</key>
<string>application/gpx+xml</string>
</dict>
</dict>
</array>
Make sure you define the UTI for GPX in your plist since it is not defined as part of the default iOS UTIs. You can do this by adding the GPX UTI to your target.
Select your Project file
select your target
select "Info"
expand "Import UTIs
Click the + icon
Add the GPX UTI info
You can also edit the plist directly if you want.
Here is what it should look like:
In the plist it will look like this:
<key>UTImportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeIdentifier</key>
<string>com.topografix.gpx</string>
<key>UTTypeReferenceURL</key>
<string>http://www.topografix.com/GPX/1/1</string>
<key>UTTypeDescription</key>
<string>GPS Exchange Format (GPX)</string>
<key>UTTypeConformsTo</key>
<array>
<string>public.xml</string>
</array>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>gpx</string>
</array>
<key>public.mime-type</key>
<string>application/gpx+xml</string>
</dict>
</dict>
</array>
Good info on this blog as well...
UPDATE
I assume you already have the document type defined as well in the plist. It should match this:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>GPS Exchange Format (GPX)</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.topografix.gpx</string>
</array>
</dict>
</array>
Also implementing CFBundleDocumentTypes to plist allows your app to open when the air drop content conforms to your UTI.
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>GPX File</string>
<key>LSHandlerRank</key>
<string>Default</string>
<key>LSItemContentTypes</key>
<array>
<string>com.your-Bundle-Idenitifer.gpx</string>
</array>
</dict>
</array>

Resources