通过网络或应用登录 Twitter 的简单方法
适用于 iOS 的 Twitter 登录
让 Twitter 用户使用内部浏览器或 Twitter 应用快速轻松地进入你的应用。
它是Twitter API的接口,登录过程轻松而清晰。
没有依赖关系,框架文件很小。
支持 iOS 12+。
开始
- 通过 Twitter 开发者应用仪表板生成你的 Twitter API 密钥。
- 准备 3 个必需参数:消费者密钥、消费者密钥、回调URL
- 使用以下说明安装库。
使用 Cocoapods 安装
若要将 TwitterSignIn 添加到你的应用,请添加到你的 Podfile。TwitterSignIn
pod 'TwitterSignIn'
使用示例
进口
import TwitterSignIn
在调用登录之前配置参数
TwitterSignIn.sharedInstance.consumerKey = "Your ConsumerKey Value"
TwitterSignIn.sharedInstance.consumerSecret = "Your ConsumerSecret Value "
TwitterSignIn.sharedInstance.callbackUrl = "Your CallbackURL Value"
注意照顾好你的消费者秘密,你不应该暴露在公众面前。
使用推特应用登录
警告不建议使用应用授权! 它由UIApplication.open(url:)实现,很容易公开consumerSecret和consumerKey。 这可能会造成未知和危险的风险。 默认情况下,使用 Twitter 应用登录处于关闭状态。
如果你确实需要使用“通过 Twitter 应用登录”,请按照以下步骤操作
1.将应用程序身份验证设置为
TwitterSignIn.sharedInstance.appAuthEnable = true
2.add twitterauth
to LSApplicationQueriesSchemes in Plist file
<key>LSApplicationQueriesSchemes</key>
<array>
<string>twitterauth</string>
</array>
3.config you CallbackURL
For use Sign-In with Twitter App, you need to set your CallbackURL to the specified value from Twitter Dashboard. It is start with and append you ConsumerKey.twitterkit-
Like:twitterkit-YouConsumerKey://
4.add the specified CallbackURL to CFBundleURLTypes in Plist file
the URLSchemes value is with out ://
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>twitterkit-YouConsumerKey</string>
</array>
</dict>
</array>
5.add handleUrl
to openUrl in App Delegate
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
TwitterSignIn.sharedInstance.handleUrl(url)
return true
}
Sign-In
If set to true, this library will try to call Twitter App first. If Twitter App not installed, will call inner browser.appAuthEnable
TwitterSignIn.sharedInstance.signIn { user, error in
if let error = error {
//handle failed with TwitterSignInError
//the library collect all error code into TwitterSignInError
//Like: if (error.code == TwitterSignInError.CancelFromApp) { ... }
return
}
//handle success from user
//user.token: for server verification
//user.secret: for server verification
//user.userId: this value return from Web auth
//user.userName: this value return from App auth
}