App extension for photos app
Our Jigsaw Puzzle Break app allows turning your photos on the device into jigsaw puzzles. The iOS platform image picker makes it easy to pick a photo from the Photos app and the best part is that it doesn't even require any special permissions for the app (the user is prompted on first use of the picker from within an app).
iPhone displays memories from the past and it is fun to see a set of memories stitched together over time. While image picker is good for better UX from within the app, what about when the user is in the Photos app and want to turn a picutre into jigsaws puzzle?
This is where the app extension comes into picture.
iOS and iPad OS are very secure preventing data from one app being accessed by another. While this is good, there may be times when this actually hinders the ability to get things done. Apps either produce and/or consume content. If one app has content such as photos and another app can do something with a photo then it is benefitial for some form of communication between both the apps. As mentioned earlier, the photo picker allows accessing the photos from an app. But often, it is also desirable to access the app when viewing photos. App Extensions allows this type of content actions and the actions that are most common are sharing.
Jigsaw Puzzle Break has been updated to make use of the app extensions so that a photo can be sent to the app. It required reading a few tutorials and looking at the API doc to finally get it done and this blog post is yet another attempt to distill it all together. Below are the steps required.
-
A new target needs to be created for the app extension. This can be done by creating a new file and using the App Extension template.
-
The new target has to be embedded into the app. What this means is, even though there are two separate targets, they are packaged together and the extension is published along with the app.
-
Because of how an app extensions works sharing the content is non-trivial. The host app (Photos app), the app extension and the app (like our Jigsaw Puzzle Break) are all running as three separate processes. So the question is how they share data among them, such as a photo. Becuase the action originates from the host app, it can choose to make content available to the extension. However, the same content is not accessible to the app itself. By default the extension can't easily communicate with the app either. But it is all possible based on a couple of platform level features
- Custom URL Scheme - First make your app work with custom urls. For example, we registered "jigsawbreak:" scheme for our app.
- App Groups - Create an application group that is shared between the app extension and the app. Then both have access to the file system that belongs to the app group. When the app extension receives an input item from the host app, it can copy it into the app group folder, create a url based on the custom URL scheme and then invoke the app with this url. This calls the app passing the URL and the app can then get hold of the file that is available in the app group's folder.
That's all there is to create a custom action to invoke your app from the photos app.