Last active
December 31, 2015 10:34
-
-
Save g-1/2e84ef9cd6c2ec318e5e to your computer and use it in GitHub Desktop.
FebricでTwitterのタイムラインを表示する(Xcode7.2版) ref: http://qiita.com/g-1/items/05e20de236fb486cb84c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import TwitterKit | |
class LoginViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
/* | |
let logInButton = TWTRLogInButton { (session, error) in | |
if session != nil { | |
//self.performSegueWithIdentifier("main", sender: session!.userName) | |
self.performSegueWithIdentifier("timeline", sender: session!.userName) | |
} else { | |
NSLog("Login error: %@", error!.localizedDescription); | |
} | |
} | |
// TODO: Change where the log in button is positioned in your view | |
logInButton.center = self.view.center | |
self.view.addSubview(logInButton)*/ | |
Twitter.sharedInstance().logInWithCompletion { session, error in | |
if (session != nil) { | |
print("signed in as \(session!.userName)"); | |
self.performSegueWithIdentifier("timeline", sender: session) | |
} else { | |
print("error: \(error!.localizedDescription)"); | |
} | |
} | |
} | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
let session = sender as? TWTRSession | |
let dest = segue.destinationViewController as! TimelineViewController | |
dest.title = session!.userName | |
dest.userId = session?.userID | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import TwitterKit | |
class TimelineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
var tableView: UITableView! | |
var tweets: [TWTRTweet] = [] { | |
didSet { | |
tableView.reloadData() | |
} | |
} | |
var prototypeCell: TWTRTweetTableViewCell? | |
var userId: String? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView = UITableView(frame: self.view.bounds) | |
tableView.delegate = self | |
tableView.dataSource = self | |
prototypeCell = TWTRTweetTableViewCell(style: .Default, reuseIdentifier: "cell") | |
tableView.registerClass(TWTRTweetTableViewCell.self, forCellReuseIdentifier: "cell") | |
self.view.addSubview(tableView) | |
loadTweets() | |
} | |
func loadTweets() { | |
TwitterAPI.getHomeTimeline(userId,tweets: { | |
twttrs in | |
for tweet in twttrs { | |
self.tweets.append(tweet) | |
} | |
}, error: { | |
error in | |
print(error.localizedDescription) | |
}) | |
} | |
// MARK: UITableViewDataSource | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
// Return the number of Tweets. | |
return tweets.count | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! TWTRTweetTableViewCell | |
let tweet = tweets[indexPath.row] | |
cell.configureWithTweet(tweet) | |
return cell | |
} | |
// MARK: UITableViewDelegate | |
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { | |
let tweet = tweets[indexPath.row] | |
prototypeCell?.configureWithTweet(tweet) | |
let height = TWTRTweetTableViewCell.heightForTweet(tweet, width: self.view.bounds.width) | |
if !height.isNaN { | |
return height | |
} else { | |
return tableView.estimatedRowHeight | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- class func getHomeTimeline(tweets: [TWTRTweet]->(), error: (NSError) -> ()) { | |
+ class func getHomeTimeline(user: String?, tweets: [TWTRTweet]->(), error: (NSError) -> ()) { | |
let api = TwitterAPI() | |
+ let client = TWTRAPIClient(userID: user) | |
var clientError: NSError? | |
let path = "/statuses/home_timeline.json" | |
let endpoint = api.baseURL + api.version + path | |
- let request:NSURLRequest? = Twitter.sharedInstance().APIClient.URLRequestWithMethod("GET", URL: endpoint, parameters: nil, error: &clientError) | |
+ let request:NSURLRequest? = client.URLRequestWithMethod("GET", | |
+ URL: endpoint, | |
+ parameters: nil, | |
+ error: &clientError) | |
if request != nil { | |
- Twitter.sharedInstance().APIClient.sendTwitterRequest(request!, completion: { | |
+ client.sendTwitterRequest(request!, completion: { | |
response, data, err in | |
if err == nil { | |
let json: AnyObject? = try! NSJSONSerialization.JSONObjectWithData(data!, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment