Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Foundation
import Swifter
@objc class SwifterWrapper: NSObject {
@objc static let share: SwifterWrapper = SwifterWrapper()
private var twitter: Swifter?
private var callbackUrl: URL?
private override init() {
}
// MARK: - キーとシークレットの設定
@objc func setup(consumerKey: String, consumerSecret: String) {
callbackUrl = URL(string: "twitterkit-" + consumerKey + "://")
twitter = Swifter(consumerKey: consumerKey, consumerSecret: consumerSecret)
}
// MARK: - スキーマのコールバックハンドリング
@objc class func handleOpen(url: URL) -> Bool {
Swifter.handleOpenURL(url)
return true
}
// MARK: - 連携
@objc func login(controller: UIViewController, success: @escaping (_ userID: String?, _ screenName: String?, _ token: String?, _ tokenSecret: String?) -> Void, failure: @escaping (_ error: Error?) -> Void) {
guard let callbackUrl = self.callbackUrl else {
failure(nil)
return
}
twitter?.authorize(withCallback: callbackUrl, presentingFrom: controller, success: { (accessToken, _) in
guard let key = accessToken?.key else {
failure(nil)
return
}
guard let secret = accessToken?.secret else {
failure(nil)
return
}
guard let userID = accessToken?.userID else {
failure(nil)
return
}
guard let screenName = accessToken?.screenName else {
failure(nil)
return
}
success(userID, screenName, key, secret)
}, failure: { (error) in
failure(error)
})
}
// MARK: - つぶやき投稿
func inviteTweet(status: String, media: Data?) {
guard let media = media else {
twitter?.postTweet(status: status)
return
}
twitter?.postTweet(status: status, media: media)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.