是否可以通过API在iOS应用程序中创建可嵌入的推文,而无需用户登录Twitter?

时间:2016-04-17 20:13:47

标签: ios cocoa-touch twitter twitter-oauth

Twitter API是否公开了这样的功能?我可以在这里和那里显示一条推文(这是一个链接保存应用程序)并通过API获取推文的信息(转推,推文等)?

我知道如果用户通过OAuth登录到我的应用程序,我可以这样做,但是如果我只是想在这里和那里显示推文的预览而不让用户登录呢?这样的事情有可能吗?

1 个答案:

答案 0 :(得分:1)

1)Install Twitter via CocoaPods

2)更新您的ConsumerKey和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)")
        }
    }
}