Search code examples
ioscocoa-touchtwittertwitter-oauth

Is it possible to create embeddable tweets in an iOS app through the API without having users log in to Twitter?


Does the Twitter API expose such functionality? Where I could display a single tweet here and there (this is a link-saving app) and get the information (retweets, tweet text, etc.) of the tweet through the API?

I know I can do this if the user is logged in via OAuth to my app, but what if I just wanted to display a preview of a tweet here and there without making the user log in? Is such a thing possible?


Solution

  • 1) Install Twitter via CocoaPods

    2) update your ConsumerKey and consumerSecret

    AppDelegate.swift

    import TwitterKit
    
    var window: UIWindow?
    let viewController = ViewController()
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.rootViewController = self.viewController
        self.window!.makeKeyAndVisible()
    
        Twitter.sharedInstance().startWithConsumerKey("################", consumerSecret: "################")
    
        return true
    }
    

    ViewController.swift

    import TwitterKit
    
    override func viewDidLoad() {
    
        super.viewDidLoad()
    
        TWTRAPIClient().loadTweetWithID("631879971628183552") { (tweet, error) in
    
            if let unwrappedTweet = tweet {
    
                let tweetView = TWTRTweetView()
                tweetView.theme = .Light
                tweetView.backgroundColor = UIColor.whiteColor()
                tweetView.primaryTextColor = UIColor.blackColor()
                tweetView.linkTextColor = UIColor.blueColor()
                tweetView.showBorder = false
                tweetView.configureWithTweet(unwrappedTweet)
                tweetView.delegate = self
                let height = TWTRTweetTableViewCell.heightForTweet(unwrappedTweet, width: self.view.bounds.size.width - 40)
                tweetView.frame = CGRect(x: 20, y: 0, width: self.view.frame.width - 40, height: height)
                tweetView.center = CGPoint(x: self.view.center.x, y: self.view.center.y)
                self.view.addSubview(tweetView)
    
            } else {
                print("Tweet load error: \(error!.localizedDescription)")
            }
        }
    }