Fabricでtwitter認証
Twitterが提供しているモバイルアプリの開発プラットフォームのFabricで簡単にtwitter認証してみた。
Fabric - Twitter's Mobile Development Platform
Fabricが提供している機能
Twitter Twitter へのサインイン, Twitter API の呼び出し, View の提供, SMS 認証
Crashlytics クラッシュ統計, リアルタイム分析, テスト配信
MoPub 広告配信による収益化
SDKインポート
SDKと今回はtwitter認証だけなのでその辺をサポートしているTwitter Kitをインポート。
pod 'TwitterKit' pod 'Fabric'
セットアップ
application didFinishLaunchingWithOptions:
Twitter Kitをイニシャライズします
import Fabric import TwitterKit func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { Twitter.sharedInstance().startWithConsumerKey("your_key", consumerSecret: "your_secret") Fabric.with([Twitter.self()]) return true }
この際、Twitterにデベロッパー登録をして、自分のアプリの ConsumerKey
と consumerSecret
を取得しておく必要があります
TwitterのAPIを使用するために必要なキーを取得する手順 - Hello API
アカウント認証
デフォルトで用意されてるボタンを使いたい時
let logInButton = TWTRLogInButton(logInCompletion: { session, error in if (session != nil) { println("signed in as \(session.userName)"); } else { println("error: \(error.localizedDescription)"); } }) logInButton.center = self.view.center self.view.addSubview(logInButton)
カスタムでメソッドを使いたいとき
Twitter.sharedInstance().logInWithCompletion { session, error in if let session = session { print(session.userName) } else { print(error) } }
これで完了。