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

[Closed] Can I share a specific View ?

Forums > SwiftUI

I was trying to save a specific View in SwiftUI, i came across ShareLink, i am however not able to share a specific View in app, like i am able to share a specific page in a web site, is there any way to achieve this , thanks

1      

Do you mean deep linking?

1      

@Hatsushira - i was looking to share the app on say watsapp etc , but the specific View in the app rather then the app in general , will deep linking work that ? If. that is not possibe, is there any API that i can use before app is launched. that picks up the app address on appstore or that kind of sharing is possible only after app is approved on appstore , thanks ..

1      

So, i was able to get this to work, it shows a share sheet - but how can i get some id of the app, which will later show the correct app address on appstore, thanks

ShareLink(item: URL(string: "https://developer.apple.com/xcode/swiftui/")!) {
                        Label("", systemImage: "square.and.arrow.up")
                            .font(.custom("Arial", size: 50))
                    }

1      

When you want to deep link to a view in an app the app has already to be installed on the device. Beforehand, this doesn't work.

I'm sure as long as the app is not released Apple's App Store wouldn't show the app to the public, either. Even if you have the link to the app store.

2      

@Thomass - thanks, i am still to go through it but i will soon, as i near completion of a project where i will need this kind of sharing

1      

Here it is how can you do it in a simple way: import SwiftUI import UIKit

struct ContentView: View { @State private var isSharing = false

var body: some View {
    VStack {
        Text("Hello, World!")
        Button(action: shareView) {
           [ Text](https://fancytextvilla.com)("Share")
        }
    }
    .sheet(isPresented: $isSharing, onDismiss: nil) {
        ShareSheet(activityItems: [captureView()])
    }
}

func captureView() -> UIImage {
    let view = UIApplication.shared.windows.first?.rootViewController?.view
    let renderer = UIGraphicsImageRenderer(size: view?.bounds.size ?? CGSize.zero)
    let image = renderer.image { context in
        view?.drawHierarchy(in: view?.bounds ?? .zero, afterScreenUpdates: true)
    }
    return image
}

func shareView() {
    isSharing = true
}

}

struct ShareSheet: UIViewControllerRepresentable { let activityItems: [Any]

func makeUIViewController(context: UIViewControllerRepresentableContext<ShareSheet>) -> UIActivityViewController {
    let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
    return controller
}

func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ShareSheet>) {
    // Empty implementation
}

}

1      

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!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.