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

[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      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

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.