File association in iOS

314 Views Asked by At

I have an iOS swift app that uses a custom file type - .csk. It creates that custom file and can edit/view for its functioning. When the user taps the .csk file, I expect the app to launch and the url of the file is passed in the open:options: delegate method

And as per my understanding, the following delegate methods will be called in order:

  1. willFinishLaunching;
  2. open:options:
  3. didFinishLaunching:

I plan to process the file using which my app was launched in didFinishLaunching (step 3), whose URL is obtained in open:options: (step 2).

I have the following delegate methods:

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
        NSLog("willFinishLaunchingWithOptions")
        
        // Do something
        
        return true
    }

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      
        NSLog("didFinishLaunchingWithOptions")
        
        // Do something
        
        return true
    }

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        NSLog("open:options")
        NSLog("File name = %@", url.path)
        
        // Do something

        return true
    }

Now, in order to associate a custom file with the app, the CFBundleDocumentTypes key (for associating app with the file) and UTExportedTypeDeclarations key (for defining a new file type - .csk) must be defined.

Here's my info.plist file:

<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeName</key>
            <string>csk file</string>
            <key>LSHandlerRank</key>
            <string>Owner</string>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>hi.csk</string>
            </array>
        </dict>
        ...
        ...

<key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeDescription</key>
            <string>My personal file type</string>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.text</string>
            </array>
            <key>UTTypeIdentifier</key>
            <string>hi.csk</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>csk</string>
                </array>
            </dict>
        </dict>

Test scenario: Launch the app using the .csk file

I use the macOS Console app to view the iOS app's logs since the app can't be started from Xcode (it will be started by tapping the .csk file).

After tapping the file, it opens in the Files app (Why does the app not get directly launched?). From there, using the open-with option, I select my app. The app starts running and I see the following logs:

willFinishLaunchingWithOptions
didFinishLaunchingWithOptions

open:options is not called and I don't see the file name anywhere (scanned all the logs - no reference to open:options:).

What have I missed?

References:

  1. I have closely followed this stackoverflow post.
  2. CFBundleDocumentTypes key and UTExportedTypeDeclarations key
  3. System UTIs
  4. File association
  5. Other Apple documents I've referred are linked above (as I've explained the situation)
0

There are 0 best solutions below