Search code examples
swiftios13twitterkit

TwitterKit Login WebView not dismissing in iOS 13


I am trying to implement a Twitter login in a new app I am working on using TwitterKit. Everything is working on iOS 11 and iOS 12, but there seems to be problem with iOS 13.

Here is my code in my LoginViewController

override func viewDidLoad() {
    super.viewDidLoad()

    let logInButton = TWTRLogInButton(logInCompletion: { session, error in
        if session != nil {
            UserDefaults.standard.set(session?.userID, forKey: "userId")
            UserDefaults.standard.set(session?.userName, forKey: "userName")
            UserDefaults.standard.set(true, forKey: "isLoggedIn")

            print("***** complete with login")
            self.performSegue(withIdentifier: "toHome", sender: self)

        } else {
            print("TWTRButton Error:")
            print(error.debugDescription)
        }
    })
    logInButton.center = self.view.center
    self.view.addSubview(logInButton)
}

After logging in, the webview that logs in is supposed to dismiss, but it just spins at this current screen and does not dismiss. In fact, the loginCompletion it's self never fires, because neither of the print statements never fire.

enter image description here

I think it has something to do with the modal presentation. Anyway around this?


Solution

  • So the issue was with changes to Xcode 11 itself. I used a maintained version of TwitterKit called TwitterKit5 and I posted an issue on the GitHub.

    https://github.com/touren/twitter-kit-ios/issues/10#issuecomment-582262246

    The maintainer pointed out that the new Xcode creates a new SceneDelegate file which blocks

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
    

    Added this to SceneDelegate.swift:

    func scene(_ scene: UIScene,
               openURLContexts URLContexts: Set<UIOpenURLContext>) {
        if let context = URLContexts.first {
            TWTRTwitter.sharedInstance().application(UIApplication.shared, open: context.url, options: [UIApplication.OpenURLOptionsKey.sourceApplication: context.options.sourceApplication as Any])
        }
    }
    

    Worked perfectly after that!