UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

1      

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.

Shortcuts

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.

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.