WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

How do I get iOS to open my app from a file?

Forums > SwiftUI

How do I set up an app to open files with a custom extension from anywhere, process them, and then discard them?

As a (not entirely accurate) example, let's pretend a friend sends me a text file in Messages. Without needing to save it anywhere, I want to know how many characters are in the file. How do I tell iOS that I should be the one to open the file, and how do I tell my app what to do with it once it opens? (Assume I know how to process the data in the file.) I don't want it to appear as a regular text document, so I don't want it to have a .txt extension.

I see hints of answers across the web. I see a lot of results for document-based apps, but mine isn't. I never need multiple documents.

An answer on Stack Overflow gives at least part of an answer for a scene-based app, but I don't think mine is. The word Scene doesn't appear anywhere in the project. (Wait, scratch that. It appears once in the <AppName>App.swift file. But it's a struct, not a class.)

I found this tutorial on raywenderlich.com, but I got lost when it came time to modify AppDelegate.swift, which my project doesn't have (and it seems to be using UIKit rather than SwiftUI)

This article at betterprogramming.pub seems closer to my project, but the intended use is to open a custom file sent to me in Messages, not to pick a file from within the application.

It feels like I'm sort of close and if I just knew the exact right words to search for I might find it, but I'm hoping to find an answer here faster.


Bob wants to automate!

How do I set up an app to open files with a custom extension from anywhere, process them, and then discard them?

I have a feeling this is a question that hackers ask. Consequently, Apple has gone to great lengths to make sure you can't do this!

But you have a legitimate business process, so what options are available to you? Break your big business problem into several smaller business problems. Try to automate each step. Then think of a strategy to weave those processes together.


Have you explored iOS Shortcuts?

See-> Communication Shortcuts

Shortcuts provides a mechanism for running code when you receive a message from a particular person and the message contains particular phrases. Start here!

Using your example, a friend sends you a text file. Use Shortcuts to look for messages sent by your friend, and maybe the phrase "Daily Sales Receipts". You might be able to take action on that message and save any attachments to a "Receipts" folder. If necessary, rename the file with a sequence number, and timestamp.

Now the problem is out of the Messages application. Another Shortcut might execute anytime there's a file in the Receipts folder. This might kick off your custom Swift app to open and parse the file, storing data in a CoreData database, or following other business processes.

When that finishes, you might have another Shortcuts, or possibly Swift application to delete the file in the Receipts folder.

Once all these pieces work, you can consider how to merge them.


Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.