To complete this project we’re going to add two more important features: letting the user share their image using SwiftUI's ShareLink
view, then adding a prompt to encourage them to review our app on the App Store – once a suitable amount of time has elapsed, of course.
Neither of these are challenging, so let's get straight into the code.
SwiftUI's ShareLink
button lets us share things like text, URLs, and images in just one line of code, and it automatically takes care of using the system-standard share sheet so that users see all the apps that support the data we're sending.
In our case, we already have a // share the picture
comment, but that needs to be replaced with a check to see if there is an image to share, and, if there is, a ShareLink
button using it.
Replace the comment with this:
if let processedImage {
ShareLink(item: processedImage, preview: SharePreview("Instafilter image", image: processedImage))
}
That's already the first step complete, although remember to try it on a real device so you can see how various real apps respond to the image.
And now all that remains is to ask the user to review our app. Remember, this is best shown only after the user has really felt the benefit of the app, because if you ask too early they are more likely to ignore the request.
So, rather than always showing the request, we're instead going to wait until the user has changed filter at least 20 times – that's enough they've gone through all the options more than once, so hopefully they are happy enough to help with a review.
First, we need to add two new properties: one to get the reviewer requester from SwiftUI's environment, and one to track how many filter changes have happened. Add another import to ContentView.swift, this time for StoreKit, then add these two properties to ContentView`:
@AppStorage("filterCount") var filterCount = 0
@Environment(\.requestReview) var requestReview
And now we need to add some code to the end of the setFilter()
method so that we increment filterCount
for each filter change, then activate the review request when we have at least 20 filter changes. Put this at the end of the method:
filterCount += 1
if filterCount >= 20 {
requestReview()
}
That will trigger an error in Xcode: requesting a review must be done on Swift's main actor, which is the part of our app that's able to work with the user interface. Although we're currently writing code inside a SwiftUI view, Swift can't guarantee this code will run on the main actor unless we specifically force that to be the case.
That sounds complicated, but really it just means changing the method to this:
@MainActor func setFilter(_ filter: CIFilter) {
Now Swift will ensure that code always runs on the main actor, and the compile error will go away.
Tip: For testing purposes, you should perhaps change the review condition from 20 down to 5 or so, just to make sure your code works the way you expect!
That final step completes our app, so go ahead and run it again and try it from end to end – import a picture, apply a filter, then share it to a different app. Well done!
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until September 29th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.