LoginSignup
4
4

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-09-08

Podinstall

pod init

Podfileに
pod 'TwitterKit'

pod install

APIキーの取得

スキームの追加

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_

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4