Display a Tweet in a TweetView using the Tweet's id.
- Using Xcode 11+ go to File > Swift Packages > Add Package Dependency
- Paste the project URL: https://github.com/estampworld/tweetview-ios
- Click on next and select the project target
TweetView.prepare()
let width = view.frame.width - 32.0
let tweetView = TweetView(id:"1345021162959503360")
tweetView.frame = CGRect(x: 16, y: 16, width: width, height: width)
tweetView.delegate = self
self.view.addSubview(tweetView)
tweetView.load()
extension ...: TweetViewDelegate {
func tweetView(_ tweetView: TweetView, didUpdatedHeight height: CGFloat) {
tweetView.frame.size = CGSize(width: tweetView.frame.width, height: height)
}
func tweetView(_ tweetView: TweetView, shouldOpenURL url: URL) {
}
}
struct SwiftUITweetView: View {
@State var id: String
@State var height: CGFloat = 200
var body: some View {
TweetViewAdapter(id: $id, height: $height)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: height, maxHeight: .infinity)
}
}
struct TweetViewAdapter: UIViewRepresentable {
@Binding var id: String
@Binding var height: CGFloat
func makeCoordinator() -> Coordinator {
return Coordinator(height: $height)
}
func makeUIView(context: Context) -> TweetView {
TweetView.prepare()
let tweetView = TweetView(id: id)
tweetView.delegate = context.coordinator
tweetView.load()
return tweetView
}
func updateUIView(_ uiView: TweetView, context: Context) {
}
}
class Coordinator: NSObject, TweetViewDelegate {
@Binding var updatedHeight: CGFloat
init(height: Binding<CGFloat>) {
_updatedHeight = height
}
func tweetView(_ tweetView: TweetView, didUpdatedHeight height: CGFloat) {
tweetView.frame.size = CGSize(width: tweetView.frame.width, height: height)
updatedHeight = height
}
func tweetView(_ tweetView: TweetView, shouldOpenURL url: URL) {
}
}
https://blog.twitter.com/developer/en_us/topics/tips/2019/displaying-tweets-in-ios-apps.html
Eduardo Irias, creator.
- Fork it ( https://github.com/estampworld/tweetview-ios/fork )
- Create your feature branch (git checkout -b feature/[my-new-feature])
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin feature/[my-new-feature])
- Create a new Pull Request
Toast Alert Views is available under the MIT license. See the LICENSE file for more info.