Add login to Twitter, Facebook and Google to your iOS app

Integrating 3 social medias onto iOS app is quite challenging, you may need to install the SDK for each one, and integrating one by one and it sure taking time. Unless if you have done it before, it might making the integration faster.

I guaranteed you might need a few minutes or maximum 1 hour to integrate those 3 accounts of Twitter, Facebook and Google. How so?

Install cocoapods

Follow the list below if you do not have cocoapods installed on your macbook. If you somehow already have cocoapods, please go to the 3rd section.

  • Open Terminal app and type: sudo gem install cocoapods
  • If you managed to get an error saying active support require ruby version blabla, type: sudo gem install activesupport -v 4.2.6
  • sudo gem install activesupport -v 4.2.6

Integrating cocoapods to your app

  • Go to your project directory on Terminal app.
  • Type pod init
  • Open the Podfile using vi: vi Podfile
  • You may remove the comment by typing dd. It will remove 1 current line. Make sure you are on line that contain a comment
  • The Podfile should be something like this:
platform :ios, '10.0'  
target '_YOUR_PROJECT_NAME_' do  
  
end
Starter of Podfile

Adding DTSocialMediaLogin to Podfile

I have made a cocoapod library to handle all the Twitter login, Facebook login, and Google Login. Now add to your Podfile.

platform :ios, '10.0'
target '_YOUR_PROJECT_NAME_' do

pod 'DTSocialMediaLogin'

end
Added DTSocialMediaLogin to Podfile

Go to Terminal app again, and make sure you are on the root directory of your app. Type pod install, and it will add required library to install.

Integrating DTSocialMediaLogin to your app

Open your AppDelegate.swift file, and add a line to the header to import the library.

import DTSocialMediaLogin

// add open url method
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        return DTSocialMediaLogin.openURL(app: app, url: url, options: options)
}
AppDelegate file

On your login page, add to the import line and create a variable of DTSocialMediaLogin to the header of the file:

import DTSocialMediaLogin

class LoginViewController: UIViewController {
	var socialLogin: DTSocialMediaLogin!
}
Add to header of the implementation View Controller

Add to your viewDidLoad method. Keep the String parameter empty if yo don't need one of them. How to get them? You could go to the Google APIs Console for Google, Facebook Developer Portal, and Twitter Developer Portal.

let settings = DTSocialMediaKeys(googleClientID: "_google_client_id_", facebookID: "_faceebook_id_", twitterAppKey: "_twitter_api_key_", twitterAppSecret: "_twitter_secret_key_")

socialLogin = DTSocialMediaLogin.setup(settings: settings)

On your button clicked, add these line. Change .Facebook to .Twitter or .Google depending on your needs.

@objc func didButtonClick(_ sender: UIButton) {
	socialLogin.login(with: .Facebook, from: viewController) { (error, user) in
    	// you will get the user detail on variable user
        // user.name, user.email, user.id, user.profileImageURL
    }
}

Add URL Scheme

Go to your Target app by clicking the Project name on the top left inside Project Navigator, and click on the Target. Point to Info tab, and go to URL Types section. On URL Schemes, type the following format:

  • Facebook: enter fb_your_facebook_appid_. Example, your Facebook ID is 123123, so type there fb123123.
  • Add another URL Type by clicking the Plus button.
  • Twitter: enter dttwitter-{your_app_key}
  • Google: Follow the tutorial on Google Sign-In for iOS, skip to the section 3. URL Type is your client ID with the order of the dot-delimited fields reversed. Example: com.googleusercontent.apps.1234567890-abcdefg

That's all!

Show Comments