• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - swift4 Firebase UI 身份验证不适用于 Twitter

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:46:43 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在使用 Firebase UI 在我的应用上登录 twitter 时遇到问题。

我已经设置了 google、facebook 和 twitter。 google 和 facebook 运行良好。

但是推特不行。

当我点击使用 Twitter 登录时,它会打开 Twitter 应用程序并显示此屏幕。 enter image description here

但是,在我点击连接按钮并返回创建应用程序后,什么都没有发生。

我现在的代码是这样的:

import UIKit
import Firebase
import FirebaseAuthUI
import FirebaseGoogleAuthUI
import FirebaseFacebookAuthUI
import FirebaseTwitterAuthUI
import FirebaseDatabaseUI
import TwitterKit

class WelcomeViewController: UIViewController {

    var authUI: FUIAuth { get { return FUIAuth.defaultAuthUI()!}}
    let providers: [FUIAuthProvider] = [FUIGoogleAuth(),FUITwitterAuth(),
                                        FUIFacebookAuth()]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)

    }

    @IBAction func startButtonTapped(_ sender: Any) {
        checkLoggedIn()
    }

    func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?) {
        if user != nil {
            self.performSegue(withIdentifier: "toNewUser", sender: nil)
        }
    }

}

extension WelcomeViewController: FUIAuthDelegate {

    func checkLoggedIn() {
        self.setupLogin()
        Auth.auth().addStateDidChangeListener{auth, user in
            if user != nil{
                print("success")
                self.performSegue(withIdentifier: "toNewUser", sender: nil)
            } else {
                print("fail")
                self.login()
            }
        }
    }

    func setupLogin() {
        authUI.delegate = self
        authUI.providers = providers
        authUI.isSignInWithEmailHidden = true
    }

    func login() {

        let authViewController = authUI.authViewController()
        self.present(authViewController, animated: true, completion: nil)
    }

}

控制台日志是这样的:

2018-11-04 14:49:28.269684+0900 ciel[8190:2052631] [BoringSSL] nw_protocol_boringssl_error(1584) [C13.1:2][0x104715070] Lower protocol stack error: 53
2018-11-04 14:49:28.272673+0900 ciel[8190:2052631] TIC Read Status [13:0x283b98a80]: 1:53
2018-11-04 14:49:28.272744+0900 ciel[8190:2052631] TIC Read Status [13:0x283b98a80]: 1:53

我在 AppDelegate 和 info.plist 中检查了 TWTRTwitter.sharedInstance().start()。我错了什么?

我意识到只有当我点击 twitter 时,它才会打开 twitter iOS 应用程序,但其他 2 个应用程序会打开浏览器。



Best Answer-推荐答案


您好,按照建议添加到您的文件中

在 plist 文件中

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>twitterkit-yourConsumerKey</string>
        </array>
    </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>twitter</string>
    <string>twitterauth</string>
</array><key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>twitterkit-yourConsumerKey</string>
        </array>
    </dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>twitter</string>
    <string>twitterauth</string>
</array>

在 AppDelegate 中

import TwitterKit

func application(_ application: UIApplication, didFinishLaunchingWithOptions 
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    TWTRTwitter.sharedInstance().start(withConsumerKey:”yourConsumerKey”, 
consumerSecret:”yourSecretKey”)
    return true
}


func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    TWTRTwitter.sharedInstance().application(app, open: url, options: options)
    return true
}

在您的登录屏幕中

import TwitterKit


btn_Twitter.addTarget(self, action: #selector(loginWithTwitter), for: .touchUpInside)


func loginWithTwitter(){
    TWTRTwitter.sharedInstance().logIn(completion: { (session, error) in
        if (session != nil) {
            print("signed in as \(session?.userName ?? "")");
            self.getEmailAddress(session:session!)
        } else {
            print("error: \(error?.localizedDescription ?? "")");
        }
    })
}


func getEmailAddress(session:TWTRSession){

    var theTwitterID:String = "No Email"
    let client = TWTRAPIClient.withCurrentUser()
    let request = client.urlRequest(withMethod: "GET",
                                    urlString: "https://api.twitter.com/1.1/account/verify_credentials.json",
                                    parameters: ["include_email": "true", "skip_status": "true"],
                                    error: nil)

    client.sendTwitterRequest(request, completion: { (response, data, connectionError) in
        if (connectionError == nil) {
            do{
                let json = try JSONSerialization.jsonObject(with: data!, options: [])
                print("Json response: ", json)
                let myDict:NSDictionary = json as! NSDictionary
                print(myDict.value(forKey: "name") ?? "")
                let screenName:String = myDict.value(forKey: "screen_name") as! String;
                let fullName:String = myDict.value(forKey: "name") as! String
                let picture:String = myDict.value(forKey: "profile_image_url") as! String
                if let emailId:String = myDict.value(forKey: "email") as? String{
                    print("Twitter Email: \(emailId)")
                    print("Twitter Screen Name: \(screenName)")
                    print("Twitter Full Name: \(fullName)")
                    print("Twitter Picture: \(picture)")
                    theTwitterID = screenName

                    let fullNameArr = fullName.characters.split{$0 == " "}.map(String.init)
                    let firstName: String = fullNameArr[0]
                    let lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil
                    print("Twitter FirstName: \(firstName)")
                    print("Twitter LastName: \(lastName!)")

                }else{
                    print("Error getting your email id. Please check permission for Twitter.")
                }

            }catch{

            }

        }else {
            print("Error: \(connectionError!)")
        }
    })
}

关于ios - swift4 Firebase UI 身份验证不适用于 Twitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138151/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap