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

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 {
    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!

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

BUY OUR BOOKS
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.