iOS Question Share and Import files with custom extension via email etc.

Branko Milosevic

Active Member
Licensed User
After combining several tutorials from Here and the Stack Overflow I ended up with a working solution and this is the minimum required to make the following work:
1. registering custom file extension so the other apps like email and messaging clients become aware of
2. registering your app and associating it with the new file extension so it will show up as an option to "Import with...", "Copy With..." or "Open With..." your custom extension file
3. sharing file with standard ActivityViewController
4. receiving file URL via email, cleaning up the Directory and the File name and copying file in your application folder.

IMPORTANT NOTE: Register your custom file extension and associate your app with it first before sending your first file over with your mailer. Your email client is not aware of your application yet and will likely associate it with some other application which can be hard to change and may involve sending files over and receiving them with another mailer to change the cashed associations. Using one mailer to send and another to receive is a way to avoid this issue. You may also need to reboot your testing device to get Icons to show up which was necessary in my case.

NOTE #2. When testing in debug mode keep your trip to the mailer and your app in the background short so the bridge does not drop it.

1. The following needs to be added to the Plist to register your custom extension:
'******VERY IMPORTANT ---->all tags must be in small caps or else....******

#PlistExtra:    <dict>
#PlistExtra:        <key>UTTypeConformsTo</key>
#PlistExtra:        <array>
#PlistExtra:            <string></string>
#PlistExtra:        </array>
#PlistExtra:        <key>UTTypeDescription</key>
'                my file description is "Impromptu Hoops File"
#PlistExtra:        <string>Impromptu Hoops File</string>
#PlistExtra:        <key>UTTypeIdentifier</key>
'                Identifier is my app's package name with the new custom extension "imp"
#PlistExtra:        <string>com.impromptuhoops.ImpromptuHoops.imp</string>
#PlistExtra:        <key>UTTypeTagSpecification</key>
#PlistExtra:        <dict>
#PlistExtra:            <key>public.filename-extension</key>
'                my custom extension is "imp"
#PlistExtra:            <string>imp</string>
#PlistExtra:        </dict>
#PlistExtra:    </dict>

2. The following needs to be added to the Plist to to register your app to open this extension.
#PlistExtra:    <dict>
#PlistExtra:       <key>CFBundleTypeIconFiles</key> 
#PlistExtra:        <array>
'                No need to specify Icons but the key (and parameters array) must be present. 
'               You can use Null array "<array/>" instead of the "<array></array>" but the
'                array must be here. App Icons will be used for the file icon and the Import With...
'#PlistExtra:            <string>320.png</string> 
'#PlistExtra:            <string>64.png</string>
#PlistExtra:        </array>
#PlistExtra:        <key>CFBundleTypeName</key>
#PlistExtra:        <string>Impromptu Hoops File</string>
'                 Uncomenting the following two lines would cause "Copy With.." action title,
'                 otherwise it is "Import With...". You can exeperiment and change "viewer" into
'               "editor" and you should get "Open With..."
'#PlistExtra:        <key>CFBundleTypeRole</key>  
'#PlistExtra:        <string>Viewer</string>
#PlistExtra:        <key>LSItemContentTypes</key>
#PlistExtra:        <array>
'             my App supports Files with my custom extension (see UTExportedTypeDeclarations)
#PlistExtra:            <string>com.impromptuhoops.ImpromptuHoops.imp</string>
'             And csv Files. 
#PlistExtra:            <string>Public.comma-separated-values-text</string>
#PlistExtra:        </array>
#PlistExtra:    </dict>

3. Sharing file with standard ActivityViewController is very simple:
Sub Process_Globals
          Private pg As Page
End Sub

Sub btnShare_click()
    Dim avc As ActivityViewController
    avc.Initialize("avc", Array("Some text of your choice to accompany the attachment",CreateFileUrl(File.DirDocuments&"/MyFolder", MyFileName)))
    avc.Show(pg, pg.RootPanel)
End Sub
4. Clicking or long clicking on an attachment in the e-mail the standard "Open with..." dialog will pop up and your app will be listed as one of the options to deal with the attached file. Clicking on your app Icon will start your app and the Application_OpenUrl sub which should be placed in the app's Main code module.
Private Sub Application_OpenUrl (Url As String, Data As Object) As Boolean
    If Url.StartsWith("file://") Then
'                          Extract and clean the file name "f" from the fileURL
        Dim f As String =Url.SubString(Url.LastIndexOf("/")+1)
        If f.Contains("%20") Then f=f.Replace("%20"," ")
'                           Extract and clean directory tree "d" pointing to the file from the fileURL
        Dim d As String = Url.SubString2(7,    Url.LastIndexOf("/")+1)
'                           Use standard File.Copy to copy the file to your app's private folder
        File.Copy(d,f,File.DirDocuments&"MyFolder", f)
    End If
        Return True
End Sub

Now heading over to do the same on the Android...