iOS Tutorial Open external files with your app

Discussion in 'iOS Tutorials' started by Erel, Feb 11, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Many applications that deal with files show a standard file handling dialog. One of the options in this dialog is to open the file with another app. This tutorial explains how you can add your app to this list and then open the file.

    For example you can use this to open CSV files received by mail in your app or from Dropbox app (useful for debugging this).

    The first step is to register the file types that you would like to handle in your app. You need to find the correct identifier. Some of the standard identifiers are listed here: https://developer.apple.com/library...fiers.html#//apple_ref/doc/uid/TP40009259-SW1

    For CSV the type is: public.comma-separated-values-text

    Use the PListExtra attribute to add the required declaration:
    Code:
    #PlistExtra:<key>CFBundleDocumentTypes</key>
    #PlistExtra:<array><dict><key>CFBundleTypeIconFiles</key><array/>
    #PlistExtra: <key>CFBundleTypeName</key><string>CSV File</string>
    #PlistExtra:<key>LSItemContentTypes</key><array>
    #PlistExtra:<string>public.comma-separated-values-text</string>
    #PlistExtra:</array></dict></array>
    When the user will click on the Open in... option and choose your app, your app will start and OpenUrl will be called:
    Code:
    Private Sub Application_OpenUrl (Url As String, Data As Object) As Boolean
       
    If Url.StartsWith("file://"Then
         
    Dim f As String = Url.SubString(7'remove the file:// scheme.
         Try
           
    Msgbox(File.ReadString("", f), "")
         
    Catch
           
    Msgbox("Error loading file""")
         
    End Try
       
    End If
       
    Return True
    End Sub
    You can run and test this in debug mode however if your program is in the background for too long (+-1 minute) then the process will be killed and later when your app will start it will wait for the debugger to connect (which will not happen).

    If you want to register a custom file type you will need to add a few more declarations. See the answer here: http://stackoverflow.com/questions/4186401/how-do-i-register-a-custom-filetype-in-ios

    The other side of this tutorial: http://www.b4x.com/android/forum/threads/open-local-files-with-external-apps.51941/
     
    Last edited: Mar 18, 2015
  2. omidaghakhani1368

    omidaghakhani1368 Well-Known Member Licensed User

    Thank you.Is there example?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Just post the two code snippets above to a new project.
     
  4. FabioRome

    FabioRome Member Licensed User

    hi erel,
    how can I do the same thing on Android ?
    thank
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread in the Android forum.
     
  6. chardenal

    chardenal Member Licensed User

    It work perfect with file.csv! thank you

    It is possible to have the same but with a custom file?

    I have file with extension ".alf" (for example data.alf)
    The file data.alf is a binary file that I receive from mail. I would like open the file data.alf in attachement in my mail with my app.

    Thank you for your help.
     
  7. JanPRO

    JanPRO Well-Known Member Licensed User

  8. Christian Abate

    Christian Abate Member Licensed User

    HI,

    How can I pass the csv instead of a msgbox into a table?
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
  10. Roycefer

    Roycefer Well-Known Member Licensed User

  11. Erel

    Erel Administrator Staff Member Licensed User

  12. Roycefer

    Roycefer Well-Known Member Licensed User

    Not yet, I wanted to see if there was a standard identifier for JSON before I tried to register a custom file type.
     
  13. Roycefer

    Roycefer Well-Known Member Licensed User

    For reference, this seems to work:
    Code:
    'To receive JSON files
        #PlistExtra:<key>CFBundleDocumentTypes</key>
        
    #PlistExtra:<array><dict><key>CFBundleTypeIconFiles</key><array/>
        
    #PlistExtra:<key>CFBundleTypeName</key><string>JSON File</string>
        
    #PlistExtra:<key>LSItemContentTypes</key><array>
        
    #PlistExtra:<string>public.json</string>
        
    #PlistExtra:</array></dict></array>
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice