OStack程序员社区-中国程序员成长平台

标题: ios - swift4 Firebase UI 身份验证不适用于 Twitter [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:46
标题: ios - swift4 Firebase UI 身份验证不适用于 Twitter

我在使用 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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4