Skip to content

Instantly share code, notes, and snippets.

@urouro-net
Last active October 23, 2017 10:58
Show Gist options
  • Save urouro-net/98ae6566acad7e5a413363defff32157 to your computer and use it in GitHub Desktop.
Save urouro-net/98ae6566acad7e5a413363defff32157 to your computer and use it in GitHub Desktop.
TwitterKit 💢
// To debug, sign out from this app.
if let session = Twitter.sharedInstance().sessionStore.session() {
Twitter.sharedInstance().sessionStore.logOutUserID(session.userID)
}
if let session = Twitter.sharedInstance().sessionStore.session() {
debugPrint("Session: \(String(describing: session))")
debugPrint("Existing: \(Twitter.sharedInstance().sessionStore.existingUserSessions())")
tweet()
} else {
// WORKAROUND: Twitterアプリがインストールされていない場合、TwitterKitがこのUINavigationControllerを勝手に閉じてしまう
// そのため勝手に閉じられてもいい透過のUIViewControllerをモーダルしておく
let dummy = UIViewController()
dummy.view.backgroundColor = UIColor.clear
dummy.modalPresentationStyle = .overCurrentContext
present(dummy, animated: true, completion: { [unowned self] in
Twitter.sharedInstance().logIn(with: self.navigationController, completion: { [weak self] session, error in
guard error == nil else {
debugPrint("Error: \(String(describing: error))")
dummy.dismiss(animated: false, completion: nil)
return
}
guard let session = session else {
// - Twitterアプリがインストールされていない場合
// - Twitterアプリから「キャンセル」で戻ってきた場合
debugPrint("Session is nil.")
dummy.dismiss(animated: false, completion: nil)
let alert = UIAlertController(title: "Twitter Error",
message: nil,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self?.present(alert, animated: true, completion: nil)
return
}
dummy.dismiss(animated: false, completion: { [weak self] in
self?.tweet()
})
})
})
}
func tweet() {
let message = "test"
let composer = TWTRComposer()
composer.setText(message)
composer.show(from: self) { result in
debugPrint("result: \(result.rawValue)")
switch result {
case .done:
break
case .cancelled:
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment