mattdonnelly / Swifter

[DEPRECATED] :bird: A Twitter framework for iOS & OS X written in Swift
MIT License
2.37k stars 387 forks source link

Swifter Authenticate doesn't seem to be working #324

Closed JerelRocktaschel closed 4 years ago

JerelRocktaschel commented 4 years ago

Hi - I am using swifter.authenticate with a callback URL and while Twitter recognizes a successful authentication, the closure block is never called. Here are my steps.

  1. Registered an app on Twitter and retrieved API/Secret keys. Also registered a callback URL of "SwifterTest://" and a website URL.

  2. Loaded latest version of Swifter to my Xcode project via Cocoapods.

pod 'Swifter', :git => 'https://github.com/mattdonnelly/Swifter.git', :tag => '2.4.0'

  1. Added a new URL Type with the callback URL to the Xcode project.

  2. Added code to the App Delegate:

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        Swifter.handleOpenURL(url, callbackURL: url)
         return true
    }
  3. Added code to ViewDidLoad in default ViewController

let swifter = Swifter(consumerKey: "API KEY", consumerSecret: "SECRET KEY")

if let callbackURL = URL(string: "SwifterTest://") {
swifter2.authorize(withCallback: callbackURL, presentingFrom: self, success: { token, _ in
              print("SUCCESS")
}, failure: { error in
              print("ERROR")
})

I am presented with the Twitter authorization screen and I enter my ID/password and after selecting Authorize, I am presented with a dialog to open in my app, which it does successfully. I also see the authentication on Twitter. However, the callback in the authorize function is never called. I see no SUCCESS or ERROR in the logs.

Feels like some sort of setting that needs to be set. Any help is greatly appreciated. Thank you for looking.

Edited for syntax highlighting

meteochu commented 4 years ago

Does the app open back up after authentication? Check if the application(:open:options:) method is actually called. I am not able to reproduce this with the sample app in Swifter.

If it is not, it is likely that you did not register the url scheme in the app so that iOS can recognize it. Something like the following should be added to your Info.plist

Screen Shot 2020-09-16 at 5 53 04 PM
JerelRocktaschel commented 4 years ago

Ok I figured it out thanks to your help. I didn't have this Scene Delegate set up and now I have a successful authentication.

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let context = URLContexts.first else { return }
        let callbackUrl = URL(string: "SwifterTest:/")!
        Swifter.handleOpenURL(context.url, callbackURL: callbackUrl)
    }

Appreciate your help, Andy.

meteochu commented 4 years ago

Awesome, glad you figured it out. Closing this now.