This time, I will introduce the code to handle when you want to share the app within the app. I often see that when you tap a cell in TableView, it transitions to Twitter and the tweet posting screen opens.
First of all, the introduction of the code.
//Text for sharing
let shareText = "Write the text you want to share here"
//Convert to a string that can be used in URL queries
guard let encodedText = shareText.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
guard let tweetURL = URL(string: "https://twitter.com/intent/tweet?text=\(encodedText)") else { return }
//Start the share screen by putting it on the URL
UIApplication.shared.open(tweetURL, options: [:], completionHandler: nil)
It's a simpler implementation than you think!
If the user has a Twitter account, it will transition to Twitter, and if it does not, it will transition to a screen prompting to create an account.
Recommended Posts