Last active
December 3, 2017 08:50
-
-
Save jazzedge/0f4484d1e24872865ed67a60b595b5b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
See: https://chariotsolutions.com/blog/post/importing-data-via-custom-file-types-in/ | |
01. Define the File Type | |
First we need to define a file type extension that our application will support. This is also known as a Uniform Type Identifier (UTI). For our purpose, we will use a filetype of 'csm' (for Chariot Solutions Mobile). The contents of this file will be some string. It could really be almost anything, but for simplicity we will use a string. | |
For a list of Apple defined UTIs, look here: http://developer.apple.com/library/ios/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1. | |
Once we know what our custom file type extension is, we can add support for it to the project by modifying the Info.plist file for the project. We are going to be the owner/exporter of the new file type, so we add the "Exported Type UTIs" key to the Info.plist file. | |
Based on the bundle key definitions found in https://developer.apple.com/.../CocoaKeys.html, we can fill in the required values. Since our example only has one file type that it is defining and the underlying data is text, we can add the system defined identifier for text to the "Conforms to UITs" item: public.text. | |
02. Next we need to add the identifier for our custom UTI. | |
Apple recommends using the reverse DNS format in order to ensure our new UTI is unique. I will be using the following: | |
com.chariotsolutions.customUTIHandler.csm | |
03. Nextwe want to identify the file extension that our app will be handling. | |
This is defined under the "Equivalent Types" node. Add a node under the equivalent types with a name of "public.filename-extension" and a value of "csm". You can optionally add a "Description" element under the Item for the exported type. | |
04.Support our new File Type | |
In order for our app to be recognized by the OS as supporting the newly defined file type, we need to add a "Document types" declaration to our Info.plist file. | |
he possible keys for the CFBundleDocumentTypes property can be found at http://developer.apple.com/library/.../CoreFoundationKeys.html.... We can add anything for the "Document Type Name" value. The handler rank will affect the order that Launch Services uses to present apps that handle this file type. Since our app is really the owner of this file type, we will use that as the handler rank. | |
05. Now we need to add the "Document Content Type UITs" that our app will handle. | |
In this case, we are only handling one type (as defined in the export key above): | |
com.chariotsolutions.customUTIHandler.csm | |
Just for fun, we will also give our file type an image icon. The specs for the image are defined in the keys document referenced above (64x64 in this case). | |
06. Now all we have to do is create a text file with a ".csm" extension and email it to our device. | |
Once there, long press the resulting icon and we should see our app listed as one of the "Open in" apps. | |
07. Once we select our app, it will launch and pass the file URL to this app delegate method: | |
func application (_ app: open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool { | |
guard url.pathExtension == "csm" else {return false} | |
// Do stuff | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment