Skip to content

Instantly share code, notes, and snippets.

View willrichman's full-sized avatar

Will Richman willrichman

View GitHub Profile
@willrichman
willrichman / guessgameevent.html
Last active August 29, 2015 14:02
Guessing Game Loop
<!DOCTYPE html>
<html>
<head>
<title>Guess the number!</title>
<style>
body {
background-color: #0A212A;
color: #A3BCC5;
@willrichman
willrichman / guessgameobject.html
Created June 11, 2014 02:40
An object-oriented guessing game.
<!DOCTYPE html>
<html>
<head>
<title>Guess the number!</title>
<style>
body {
background-color: #0A212A;
color: #A3BCC5;
@willrichman
willrichman / segue.swift
Created October 8, 2014 23:41
Prepare for Segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "singleTweetSegue" {
var destination = segue.destinationViewController as SingleTweetViewController
var indexPath = tableView.indexPathForSelectedRow()
tableView.deselectRowAtIndexPath(indexPath!, animated: true)
var tweetToDisplay = self.tweets![indexPath!.row] as Tweet
destination.tweetShown = tweetToDisplay
}
}
@willrichman
willrichman / fetchTimeLine.swift
Created October 9, 2014 21:40
fetch timeline
func fetchTimeLine (timelineType: String, userScreenname: String?, completionHandler: (errorDescription : String?, tweets : [Tweet]?) -> Void) {
let accountStore = ACAccountStore()
let accountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
/* The following is asynchronous.
We will ask for account access, set up a twitter request, then call the home timeline */
accountStore.requestAccessToAccountsWithType(accountType, options: nil) { (granted: Bool, error: NSError!) -> Void in
if granted {
@willrichman
willrichman / gesturerecognize.swift
Created October 9, 2014 21:57
Gesture recognizer on my profile image
override func viewWillAppear(animated: Bool) {
self.singleTweetUserName.text = self.tweetShown!.profileName
self.singleTweetTwitterHandle.text = ("@\(self.tweetShown!.screenName)")
self.singleTweetText.text = self.tweetShown!.text
self.singleTweetProfileImage.image = self.tweetShown!.profileImage
self.singleTweetFavoritesCount.text = String(self.tweetShown!.favoriteCount)
self.singleTweetRTsCount.text = String(self.tweetShown!.retweetCount)
let press = UITapGestureRecognizer(target: self, action: Selector("segueAction:"))
self.singleTweetProfileImage.addGestureRecognizer(press)
@willrichman
willrichman / twitterbigger.swift
Created October 10, 2014 00:09
Pull bigger twitter images
/* Takes the URL string and changes it to pull Twitter's bigger profile image.
Courtesy of Cameron Klein (github.com/cameronklein) */
let normalRange = smallProfileImageURL.rangeOfString("_normal", options: nil, range: nil, locale: nil)
self.profileImageURL = smallProfileImageURL.stringByReplacingCharactersInRange(normalRange!, withString: "_bigger")
@willrichman
willrichman / Refreshcontrol.swift
Last active August 29, 2015 14:07
Refresh control!
/* Taken from stackOverflow http://stackoverflow.com/questions/24475792/how-to-use-pull-to-refresh-in-swift */
self.refreshControl = UIRefreshControl()
self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl?.addTarget(self, action: "refreshTweets:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(self.refreshControl!)
func refreshTweets (sender: AnyObject) {
NetworkController.controller.fetchTimeLine(timelineType, isRefresh: true, newestTweet: self.tweets?[0], userScreenname: userTimelineShown) { (errorDescription, tweets) -> Void in
if errorDescription != nil {
@willrichman
willrichman / tweet.swift
Created October 17, 2014 21:44
Tweet a photo!
//MARK: - Social Methods
func tweet(message: String?, image: UIImage?, url: NSURL?) {
let accountStore = ACAccountStore()
let accountType = accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
accountStore.requestAccessToAccountsWithType(accountType, options: nil) { (granted: Bool, error: NSError!) -> Void in
if granted {
let accounts = accountStore.accountsWithAccountType(accountType)
self.twitterAccount = accounts.first as? ACAccount
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {
@willrichman
willrichman / search.swift
Created October 22, 2014 21:05
Search function resusable
func search (searchString: String, type: SearchType, completionHandler: (returnedArray: [AnyObject], errorDescription: String?) -> Void) {
let searchReposURL = "https://api.github.com/search/repositories?q="
let searchUsersURL = "https://api.github.com/search/users?q="
let cleanedSearch = searchString.stringByReplacingOccurrencesOfString(" ", withString: "+", options: nil, range: nil)
var url : NSURL?
switch type {
case .Repos:
url = NSURL(string: (searchReposURL + cleanedSearch))?
case .Users:
url = NSURL(string: (searchUsersURL + cleanedSearch))?
@willrichman
willrichman / gist:0c12a93ad4bd1c609f0e
Created November 18, 2014 18:23
POST and JSON into data
- (void)postDot: (Dot*)dot completionHandler: (void (^)(NSString *error, bool success))completionHandler {
NSString *fullURLString = [NSString stringWithFormat: @"%@dots/", self.url];
NSURL *fullURL = [NSURL URLWithString:fullURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fullURL];
request.HTTPMethod = @"POST";
NSData *dotJSONData = [dot parseDotIntoJSON];
NSUInteger length = dotJSONData.length;
[request setValue:[NSString stringWithFormat:@"%li", length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
request.HTTPBody = dotJSONData;