- Begin by creating a new project in Xcode.
- Select "Create a new Xcode project" near the bottom left of the window.
- Ensure that the "iOS" tab at the top of the page is selected.
- Double-click on "Single View App".
- Name your project "Twitterando".
- Pick whatever Team, Organization Name and Organization Identifier that you want.
- Ensure that the Language field is set to "Swift".
- Ensure that "Use Core Data" is unchecked.
- Click "Next".
- On the following window, select where you'd like your project's folder to be create (default location is ~/Desktop) and click "Create".
CocoaPods is the dependency manager for iOS projects. You can use it to access and integrate thousands of external Swift libraries into your projects which can scale them elegantly.
-
Open your Terminal, and run the command:
$ sudo gem install cocoapodsNote: The '$' is not a part of the command; it's merely there just to say that this line of code is a shell command explicitly.
-
After the installation has completed, run the command:
$ gem which cocoapodsto check if the installation was successful. Your terminal should output a path that ends with the file cocoapods.rb.
-
Using the cd command, navigate to your project folder. If it's stored on your Desktop (and named Twitterando), the command:
$ cd ~/Desktop/Twitterandoshould work.
-
Once inside your project's folder, run the command:
$ pod initThis will create a file named Podfile inside the same folder.
-
Edit the Podfile by opening it using any text editor. Here are some convenince commands:
TextEdit
$ open -a TextEdit PodfileSublime Text
$ open -a "Sublime Text" Podfilevim
$ vim PodfileGNU Emacs
ew who uses Emacs -
Locate the line "# Pods for Twitterando".
-
Directly underneath, insert the line:
pod 'TwitterKit'
-
Save your changes to the Podfile.
-
Return to the Terminal, and ensure you are still inside your project's folder/directory.
-
Run the command:
$ pod install -
If you run the ls command, you'll notice that the
pod installcommand will have generated a file named Twitterando.xcworkspace. From now on, to work on your project, you must use this xcworkspace file. -
Return to Xcode and close the Twitterando project.
-
Through Finder or the Terminal, open the file Twitterando.xcworkspace.
$ open -a Xcode Twitterando.xcworkspace
And just like that, we've successfully added the TwitterKit library to our project!
Back in Xcode, you should now notice a new object named Pods inside the Project Navigator on the left side of the screen. This is another insurance that your pod install command worked.
- Using the Project Navigator on the left side of Xcode, select the Main.storyboard file to open the Interface Builder. If you cannot see the Main.storyboard file, use the arrows next to the folders to expand them.
- Click inside of the single View Controller inside the Interface Builder.
- Open the Atributes Inspector by clicking on the icon to the left of the ruler icon in the top-right corner of Xcode.
- Ensure that the header in the Attributes Inspector says View
- Locate the "Background" field about six fields down from the top. Click on the white bar (not the blue arrows) to open the color picker.
- Select the second tab in the color pickers and use the sliders to change the background color to the color (192, 222, 237):
- Close the color picker.
- In the pane on the bottom right of Xcode, ensuring the third tab is selected scroll to the very bottom so that you see the Button element.
- Drag a Button into the View Controller and position it similar to the following photo:
- With the Button still selected, click on the "Resolve Auto Layout Constraints" button near the bottom right of Xcode.
- Click the topmost button that says "Reset to Suggested Constraints".
- With the Button still selected, open the Attributes Inspector by clicking on the button to the left of the ruler icon near the top-right of Xcode.
- Locate the "Title" field about four fields down from the top of the pane.
- Change the button title from "Button" to "Generate". Press Enter.
- Click on the two overlapping circles near the top right of Xcode to open the Assistant Editor.
- If it is difficult to see our View Controller, close the left pane if necessary by clicking on the button (square with black rectangle on the left side) to the left of "View as iPhone..." in the bottom left of the window.
- While holding down control (not to be confused with cmd), click on the Button and drag to the space just above
viewDidLoad(), but still within theclass ViewControllerblock. - In the window that appears, ensure that the "Type" field says UIButton, else press "Cancel" and try again.
- Change the "Connection" from Outlet to Action via the drop-down menu.
- In the "Name" field, type "tappedGenerate".
- Change the "Type" from Any to UIButton.
- Ensure the "Event" says "Touch Up Inside".
- Click "Connect"
- Close the Assistant Editor by clicking on the "X" on the top right of the pane with our code in it.
-
Using the Project Navigator, select the AppDelegate.swift file.
-
First, import TwitterKit into the AppDelegate.swift by adding the following line to the top of the file:
import TwitterKit -
Next, Change the first function,
application(_:didFinishLaunchingWithOptions:), to the following:func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. TWTRTwitter.sharedInstance().start(withConsumerKey: "qsSBF4peypKJlnkEP8fvRvBUr", consumerSecret: "J3uFqQghjrnn406cuVLXpRYkyh1qsNBmBkaL4F5NPbBaFkapBi") return true }
pls don't use our app secret for malicious intents thx :')
-
Using the Project Navigator, select the ViewController.swift file.
-
Underneath the
import UIKitline, outside of the class declaration of ViewController add the following line:import TwitterKit -
Inside the class declaration of ViewController, just above the
tappedGenerate(_:)function we created, declare the following property:var tweetView: TWTRTweetView! -
Change
viewDidLoad()to the following:override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. tweetView = TWTRTweetView() tweetView.center = self.view.center self.view.addSubview(tweetView) }
-
Change
tappedGenerate(_:)to the following:@IBAction func tappedGenerate(_ sender: UIButton) { // Generate random number from 0 ... 1b let randomID = arc4random_uniform(1000000000) TWTRAPIClient().loadTweet(withID: String(randomID)) { tweet, error in if let t = tweet { self.tweetView.removeFromSuperview() self.tweetView = TWTRTweetView(tweet: t) self.tweetView.center = self.view.center self.view.addSubview(self.tweetView) } else { print("Failed to load Tweet: \(error?.localizedDescription)") } } }