Installation

Rajul Arora edited this page Jul 17, 2018 · 17 revisions

Obtain Application Key & Secret

Twitter requires that all API requests be authenticated with tokens that can be traced back to an individual Twitter App. If you already have keys for a Twitter app you can pass them directly to the TWTRTwitter.sharedInstance().start() method.

To create a new Twitter app or use existing Twitter app, visit Twitter apps dashboard and copy the keys from the "Keys and Access Tokens" tab of your app page.

To add call back URL:

  1. In Twitter apps dashboard, find your application and go to the permissions tab.
  2. Select the appropriate permissions for your needs (e.g. "Read and write")
  3. If you are using login, add a placeholder URL in the Callback URL field (eg. "http://placeholder.com").
  4. Click update settings.

NOTE: Although the callback URL will not be requested by Twitter Kit in your app, it must be set to a valid URL for the app to work with the SDK.

Install Twitter Kit Using CocoaPods

To add Twitter Kit to your app, simply add the TwitterKit pod to your Podfile:

target 'MyApp' do
    use_frameworks!
    pod 'TwitterKit'
end

Then in your terminal run pod install, and finally pod update TwitterKit to make sure you have the latest version.

If you don't have have CocoaPods set up for your app, you can run pod init from the root of your project directory.

Note: From v3.4.0 and higher, please make sure to remove all references to TwitterShareExtensionUI in your project settings if they are not already removed by Cocoapods. You may notice a TwitterShareExtensionUI resource bundle file in the "Build Phases" section of your project section, please remove this.

Install Twitter Kit Using Carthage

To install TwitterKit for iOS using Carthage, add the following lines to your Cartfile. For more information about how to set up Carthage and your Cartfile, see here.

binary "https://ton.twimg.com/syndication/twitterkit/ios/TwitterKit.json"
binary "https://ton.twimg.com/syndication/twitterkit/ios/TwitterCore.json"

After running carthage update, make sure the following is set up in your project settings:

  • Add TwitterKit.framework to the "Linked Frameworks and Binaries" section under "General" of your App target. (Note: You might run into issues if TwitterCore.framework is also placed here).
  • Make sure TwitterKit.framework is under the "Link Binary With Libraries" under "Builds Phases". (Note: Again, you might run into issues if TwitterCore.framework is also placed here).
  • Make sure that when you are adding the copy-frameworks run script for Carthage that you add the following input paths:
$(SRCROOT)/Carthage/Build/iOS/TwitterCore.framework
$(SRCROOT)/Carthage/Build/iOS/TwitterKit.framework

Make sure that the run script phase is after your Link Binaries with Libraries phase to prevent issues with properly archiving your iOS application.

Note: From v3.4.0 and higher, please make sure to remove all references to TwitterShareExtensionUI in your project settings (Carthage does not do this for you). You may notice a TwitterShareExtensionUI resource bundle file in the "Build Phases" section of your project section, please remove this.

Install Twitter Kit Manually

  1. Download and unzip Twitter Kit.
  2. Drag contents to the root of your project in Xcode
  3. Add TwitterKit to "Embedded Binaries" in your Xcode project settings
  4. Add TwitterKit and TwitterCore to "Linked Frameworks and Libraries" in your Xcode project settings

NOTE: Make sure 'Copy items if needed' is checked when prompted for options in Xcode.

Initialize Twitter Kit

Make sure to import the framework header: #import <TwitterKit/TWTRKit.h>

If using Swift you will need to add it to your Objective-C bridging header

Inside your App Delegate, initialize Twitter Kit with your application key and secret (paste your own key and secret).

// Objective C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    [[Twitter sharedInstance] startWithConsumerKey:@"hTpkPVU4pThkM0" consumerSecret:@"ovEqziMzLpUOF163Qg2mj"];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options
{
    return [[Twitter sharedInstance] application:app openURL:url options:options];
}
// Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    TWTRTwitter.sharedInstance().start(withConsumerKey:"hTpkPVU4pThkM0", consumerSecret:"ovEqziMzLpUOF163Qg2mj")
    return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    return TWTRTwitter.sharedInstance().application(app, open: url, options: options)
}

Configure Info.Plist

Twitter Kit looks for a URL scheme in the format twitterkit-<consumerKey>, where consumerKey is your application's Twitter API key, e.g. twitterkit-dwLf79lNQfsJ.

You can find your consumer key in the Twitter app dashboard.

In your app's Info.plist, add URL Schemes by adding code below after <dict>

// Info.plist
<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>twitterkit-<consumerKey></string>
    </array>
  </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>twitter</string>
    <string>twitterauth</string>
</array>

Next Steps

Once Twitter Kit has been installed, see the following sections for further examples:

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.