[swift5] How to implement the Twitter share function

Posted content

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.

Implementation code

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!

Remarks

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

[swift5] How to implement the Twitter share function
[Swift] How to implement the countdown function
[Swift] How to implement the LINE login function
[Swift] How to implement the fade-in / out function
[Swift] How to implement the Twitter login function using Firebase UI ②
How to implement the breadcrumb function using gretel
[For beginners] How to implement the delete function
How to add the delete function
[Swift] I tried to implement the function of the vending machine
How to implement TextInputLayout with validation function
[Swift5] How to implement standby screen using'PKHUD'
[Processing × Java] How to use the function
[Swift5] How to implement animation using "lottie-ios"
[Behavior confirmed in December 2020] How to implement the alert display function
How to add sound in the app (swift)
[Swift] How to link the app with Firebase
[Rails] How to create a Twitter share button
How to implement the email authentication function at the time of user registration
Rails learning How to implement search function using ActiveModel
[Swift] How to get the document ID of Firebase
How to add ActionText function
[Swift] How to use UserDefaults
How to use Swift UIScrollView
[Swift] Processing to share screenshots
[Rails] How to implement scraping
[Java] How to implement multithreading
I tried to implement the like function by asynchronous communication
How to transition from the [Swift5] app to the iPhone settings screen
How to get the longest information from Twitter as of 12/12/2016
How to implement infinite scrolling (page nate) in Swift TableView
How to use the form_with method
How to find the average angle
How to use the wrapper class
Try to implement iOS14 Widget function
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
[Swift 5] Implement UI for review function
[Swift] How to send a notification
[Rails] How to implement star rating
[Swift] How to replace multiple strings
[Swift] How to dynamically change the height of the toolbar on the keyboard
[Swift] How to display when the quiz app answers correctly [Beginner]
[Swift5] How to get an array and the complement of arrays
I tried to implement the image preview function with Rails / jQuery
[Swift UI] How to get the startup status of the application [iOS]
[Rails] How to put a crown mark on the ranking function
How to place and share SwiftLint config files on the server
How to use MinIO with the same function as S3 Use docker-compose
A story about using the CoreImage framework to erase stains with Swift and implement a blur erase function
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
[Swift] Copy the character string to the clipboard
[swift5] How to specify color in hexadecimal
How to share files with Docker Toolbox
Shorten the UUID to base64 in Swift.
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
How to implement search function with rails (multiple columns are also supported)
How to return a value from Model to Controller using the [Swift5] protocol
How to delete the wrong migration file
[Spring Boot] I investigated how to implement post-processing of the received request.