Help us understand the problem. What is going on with this article?

【メモ】TwitterKitを使ってサインイン処理をする

More than 1 year has passed since last update.

Podinstall

pod init

Podfileに
pod 'TwitterKit'

pod install

APIキーの取得

https://apps.twitter.com/

スキームの追加

info.plist ファイルをSourcezCodeで開き,以下を記載する

image.png

ConsumerKeyを記載するのを忘れずに

info.plist
<array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>twitterkit-ここにConsumerKeyを記載</string>
            </array>
        </dict>
    </array>
    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>twitter</string>
        <string>twitterauth</string>
    </array>

表示をPropertyListに戻した際に、URLtypes→item0→URLSchemes→item0に変更が反映されてることを確認する

image.png

ApiKeyの記載

AppDelegate.Swift
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if TWTRTwitter.sharedInstance().application(app, open: url, options: options) {
            return true
        }
        // Your other open URL handlers follow […]
        return false
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        TWTRTwitter.sharedInstance().start(withConsumerKey: "ここにConsumerKeyを記載", consumerSecret: "ここにConsumerSecretを記載")
        return true
    }

Login処理の実行

(ログイン処理を実行したい)ViewController.Swift
TWTRTwitter.sharedInstance().logIn(completion: { (session, error) in
            if let sess = session {
                print("signed in as \(sess.userName)");
                print(self.username)
            } else {
                print("error: \(error?.localizedDescription)");
            }
        })

わーい!!!

IMG_6525.PNG
@Satopppy_

Satopppy
よくわからない過去ログしか残ってません N.H.S → DHU / OB of Life is Tech!
https://sapy.pw/
nnn-school
IT×グローバル社会を生き抜く“創造力”を身につけ、世界で活躍する人材を育成する。
https://nnn.ed.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away