TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Sharing an image using ShareLink

Paul Hudson    @twostraws   

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 {

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!

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.