Twitter Login in iOS App using swift 4.
Most of the time we provide the social login in app that will help user to login in app fast & smooth .
Here i have taken example of Twitter .
1: First try to follow this steps and add their SDK in App.
https://github.com/twitter/twitter-kit-ios/wiki/Installation
2: After that Go to Appdelgate.swift file and add this:
1 2 3 4 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { TWTRTwitter.sharedInstance().start(withConsumerKey:"xxxxxxxxxxxxxxxxxxxxx", consumerSecret:"ssabdavhjdafvdhjavdhjavdahjdvahdvahdvahjd") } |
Now add this method also in Appdelegate.swift file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool { var valueTwitter: Bool = true valueTwitter = TWTRTwitter.sharedInstance().application(app, open: url, options: options) return valueFacebook } func application(_ application: UIApplication, shouldAllowExtensionPointIdentifier extensionPointIdentifier: UIApplicationExtensionPointIdentifier) -> Bool { if (extensionPointIdentifier == UIApplicationExtensionPointIdentifier.keyboard) { return false } return true } func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { var handle: Bool = true let options: [String: AnyObject] = [UIApplicationOpenURLOptionsKey.sourceApplication.rawValue: sourceApplication as AnyObject, UIApplicationOpenURLOptionsKey.annotation.rawValue: annotation as AnyObject] handle = TWTRTwitter.sharedInstance().application(application, open: url, options: options) return handle } |
3: Now write the code that will fetch user email & Name .
for this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
@IBAction func tapTwitter_btn(_ sender: Any) { TWTRTwitter.sharedInstance().logIn { (session, error) in if (session != nil) { self.firstName = session?.userName ?? "" self.lastName = session?.userName ?? "" let client = TWTRAPIClient.withCurrentUser() client.requestEmail { email, error in if (email != nil) { print("signed in as \(String(describing: session?.userName))"); let firstName = session?.userName ?? "" // received first name let lastName = session?.userName ?? "" // received last name let recivedEmailID = email ?? "" // received email }else { print("error: \(String(describing: error?.localizedDescription))"); } } }else { print("error: \(String(describing: error?.localizedDescription))"); } } } func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return TWTRTwitter.sharedInstance().application(app, open: url, options: options) } |
4: Now you have done all the required things for fetching the user credential in App
5: Now last important things that open the twitter developer site and select your app (where you have registered your iOS app)
1.1: Now you have to call back URL for App.
2.1: To get the call back URL
a: open info.plist file and copy URL schemes.
it look like this.
copy this ” twitterkit-js5zHyDKTz2RTthfgV5xl9icA”
Note: ” twitterkit- Your Consumer Key”
and Now open your twitter developer account & select your app.
3.1: edit the App account .
4.1: go to callback URLs section and paste it.
like that.
5.1:Now save this.
6: Now you are able to fetch the user data without any issue.
if you have any issue regarding twitter login than freely to ask , we will wait for your valuable feedback