How can I get my SwiftUI app to show on the IOS Photos Share Sheet?

1.4k views Asked by At

I am trying to get my SwiftUI app to be listed on the share sheet of IOS photo library, so the user can directly open the application with this image.

Example: Apps like facebook, WhatsApp and Instagram do show up on the share sheet and I want my app to be on that list as well.

I was successful on showing my app on the share sheet of an image when opened from the "Files" but not working on the Photos Library.

I have added these lines of code in my app's Info.plist

    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>myapp image</string>
            </array>
            <key>CFBundleTypeName</key>
            <string>imageData</string>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>LSHandlerRank</key>
            <string>Alternate</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>public.png</string>
                <string>public.jpeg</string>
                <string>public.heic</string>
            </array>
        </dict>
    </array>


<key>UTImportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.image</string>
            </array>
            <key>UTTypeDescription</key>
            <string>PNG image</string>
            <key>UTTypeIconFiles</key>
            <array/>
            <key>UTTypeIdentifier</key>
            <string>public.png</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>png</string>
                </array>
            </dict>
        </dict>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.image</string>
                <string>public.data</string>
            </array>
            <key>UTTypeDescription</key>
            <string>JPEG image</string>
            <key>UTTypeIconFile</key>
            <string>public.jpeg.icns</string>
            <key>UTTypeIconFiles</key>
            <array/>
            <key>UTTypeIdentifier</key>
            <string>public.jpeg</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>jpeg</string>
                    <string>jpg</string>
                </array>
            </dict>
        </dict>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.image</string>
                <string>public.data</string>
            </array>
            <key>UTTypeDescription</key>
            <string>HEIC image</string>
            <key>UTTypeIconFiles</key>
            <array/>
            <key>UTTypeIdentifier</key>
            <string>public.heif-standard</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>heic</string>
                </array>
            </dict>
        </dict>
    </array>

2

There are 2 answers

1
Why_So_Ezz On

To get your SwiftUI app to show up in the iOS Photos share sheet, you will need to do the following:

Add the "Photos" entitlement to your app's entitlements file. This will allow your app to access the user's photo library.

Implement the UIActivityViewController class in your app. This class provides a standard interface for sharing content with other apps, including the Photos app.

In your SwiftUI view, add a button or other control that will trigger the share sheet. When the user taps this control, present an instance of UIActivityViewController modally.

In the initializer for UIActivityViewController, set the activityItems parameter to an array of the items you want to share (e.g. a UIImage or URL object).

In the initializer for UIActivityViewController, set the applicationActivities parameter to an array of custom UIActivity objects, if you want to include custom actions in the share sheet.

Present the UIActivityViewController modally when the user taps the share button.

struct ContentView: View {
var body: some View {
    Button(action: {
        // Load the image that the user wants to share
        let image = UIImage(named: "example.jpg")!
        
        // Create an instance of UIActivityViewController
        let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
        
        // Present the share sheet modally
        UIApplication.shared.windows.first?.rootViewController?.present(activityViewController, animated: true, completion: nil)
    }) {
        Text("Share")
    }
}

}

1
Ori On

Overview

Using the iOS share sheet, users can launch your messaging app instantly from a list of suggestions when sharing content like a link, image, video, or file. The share sheet suggests conversations with people in apps that the user interacts with frequently, and updates its suggestions over time based on the user’s favourite apps and conversations. When a user shares an image in iOS 16 or later, the share sheet prioritizes conversations with people it identifies in the image.

How to implement:

Supporting suggestions in your app’s share extension

Good luck!