Give your simulator superpowers

Give your Xcode
Simulator extra features

Shared with You implementation and testing explained

iOS 16 introduced Shared with You allowing you to showcase content shared in Messages inside your app. Users can find back content they would otherwise lose in the history of a long conversation. Apps like Photos, Music, and Podcasts have already implemented support, and now it’s time to add support to your apps.

Shared with You matches content shared with URLs based on universal links. If the matching app for universal links supports this feature, the content might appear inside the app. Let’s dive into the implementation, testing, and when you can expect shared URLs to appear.

How does Shared with You work?

Using the Shared with You framework, you can elevate content shared in Messages inside your app, allowing users to continue the messaging experience without leaving the app. You will receive a so-called SWHighlight item that you can assign to a SWAttributionView which will show details around the conversation in which the user shared the content. Users can tap the attribution view that will deeplink them back into the discussion:

Shared with You as demonstrated inside the WeTransfer app.

When does content show up inside Shared with You?

Your users control whether content shows up in Shared with You. Users can pin content and mark it as important, elevating the content into Shared with You.

Siri might as well suggest content on the shelf. Content from Siri will appear as the first item in the list, followed by pinned items and the remainder of the list.

Users can disable showing content in Shared with You per conversation through the chat’s settings. Inside Messages system settings, there are options for the user to disable automatic sharing globally or per app.

Shared with You system settings allowing the user to disable automatic sharing.
System settings allow the user to disable automatic sharing.

When the user disables automatic sharing, only pinned content will shop up inside your app.

Adding Shared with You support to your app

You start adding Shared with You to your app starts by adding support for universal links. Universal links allow users to open content directly into your app. For example, the WeTransfer app supports opening wetransfer.com links pointing to file transfers directly in the app. In this article, I will focus solely on Shared with You support. If you didn’t add universal links to your app yet, I encourage you to read through Apple’s documentation.

Determining where to show the Shared with You shelf

Apple recommends offering Shared with You items on a dedicated shelf inside your app. For example, I’ve implemented the shelf inside the Explore tab of Stock Analyzer:

An example of a Shared with You shelf inside Stock Analyzer.
An example of a Shared with You shelf inside Stock Analyzer.

While Apple recommends creating a dedicated shelf, you can decide to integrate the SWAttributionView showing the “from <user>” view in an existing list. We did this for the WeTransfer app:

Shared with You as implemented inside an existing list overview.
Shared with You as implemented inside a current list overview.

You can decide what works out best depending on your app’s structure. Make sure to configure the title using the highlightCollectionTitle property which will return a localized title that the user might recognize.

Monitoring for incoming highlights

Start your implementation by adding the Shared with You capability to your project:

Add the Shared with You capability in Xcode.
Add the Shared with You capability in Xcode.

After that, you can start monitoring incoming highlights by creating a SharedWithYouMonitor:

final class SharedWithYouMonitor: NSObject {
    private let highlightCenter: SWHighlightCenter

    init() {
        self.highlightCenter = SWHighlightCenter()
        super.init()
        highlightCenter.delegate = self
    }
}

extension SharedWithYouMonitor: SWHighlightCenterDelegate {
    func highlightCenterHighlightsDidChange(_ highlightCenter: SWHighlightCenter) {
        highlightCenter.highlights.forEach { highlight in
            print("Received a new highlight with URL: \(highlight.url)")
        }
    }
}

The highlights contain an identifier and the universal link matching the highlight. You can use this URL property to fetch any additional metadata for your app. For example, WeTransfer uses that URL to bring metadata for the matching transfer.

Adding the SWAttributionView

Once you have the SWHighlight instance and the metadata matching your highlight, you can start adding the SWAttributionView to your app. The SWAttributionView acts like a container for the Messages ‘pill’ that allows users to deeplink back into the conversation for the content.

Inside Stock Analyzer, I made use of SwiftUIKitView to wrap the attribution view quickly.

UIViewContainer(SWAttributionView(), layout: .intrinsic)
    .set(\.highlight, to: sharedSymbol.highlight)
    .set(\.horizontalAlignment, to: .leading)
    .set(\.displayContext, to: .summary)
    .set(\.preferredMaxLayoutWidth, to: 200)
    .frame(maxWidth: .infinity)

Unfortunately, there isn’t a SwiftUI implementation for the attribution view available by default.

A similar implementation in UIKit looks as follows:

let attributionView = SWAttributionView()
attributionView.horizontalAlignment = .leading
attributionView.displayContext = .summary
attributionView.highlight = highlight
attributionView.menuTitleForHideAction = "Delete Transfer"

The horizontal alignment property aligns the Messages pill inside the available width. You’ll only need the preferredMaxLayoutWidth property inside SwiftUI to correctly set the frame. You can use the displayContext to inform the system in which environment your app will show the attribution view. It would be best if you use summary inside lists and detail when the attribution view shows up inside your app’s detail page.

You can use the menuTitleForHideAction to adjust the title of the context menu that’s shown when a user long presses the attribution view. For WeTransfer, it made sense to call it “Delete Transfer.”

Testing and troubleshooting Shared with You content

During the implementation of Shared with You inside my apps, I had trouble getting content to show up. There are a few essential parts to verify that could be a reason for content to not show up inside your app:

  • Make sure to configure the SWHighlightCenterDelegate. You won’t receive any content without it.
  • Turn on Automatic Sharing globally and for your app inside system settings.
  • Test on a physical device; highlights won’t appear inside the Simulator.
  • While pinning content shouldn’t be necessary, it can help evaluate your app’s content.
  • Links should be shared by friends or family, but in my experience, it’s enough to ensure you have a conversation in Messages with a known number. In other words, ensure to start a conversation with somebody from your contacts list.
  • Ensure you’ve added the Shared with You capability.

Hopefully, these tips will help you to enable content to show up inside your app.

Conclusion

Adding Shared with You to your app helps your users to find back content that potentially got lost inside a Messages conversation. The attribution view allows users to deeplink back into a relevant conversation without leaving your app.

If you like to improve your Swift knowledge, check out the Swift category page. Feel free to contact me or tweet me on Twitter if you have any additional tips or feedback.

Thanks!

 

Featured SwiftLee Jobs

Loading RSS Feed

Browse more Swift related Jobs, or add your own on SwiftLee Jobs