Paweł Tarszewski
Paweł Tarszewski

Reputation: 71

Getting errors from Twitter.sharedInstance() Swift 3 iOS 10

I am writing app with Swift 3 on iOS 10. sharedInstance() method throws errors to console when user deny permissions to account from systems or account is not configured (e.g. "Unable to authenticate using the system account"). Errors are shows on console before enter to closure. I wont shows this errors to users in app e.g. on alert. This is my code:

  Twitter.sharedInstance().logIn { (session, error) in
                if error != nil {
                //   print(error?.localizedDescription ?? "    ")
                    return
                })

I get this error:

2016-11-29 14:49:09.023 CarReview[1254:31719] [TwitterKit] did encounter error with message "Unable to authenticate using the system account.": Error Domain=TWTRLogInErrorDomain Code=2 "User allowed permission to system accounts but there were none set up." UserInfo={NSLocalizedDescription=User allowed permission to system accounts but there were none set up.}
2016-11-29 14:49:09.024 CarReview[1254:31719] [TwitterKit] No matching scheme found.
2016-11-29 14:49:09.292 CarReview[1254:31719] [TwitterKit] did encounter error with message "Error obtaining user auth token.": Error Domain=TWTRLogInErrorDomain Code=-1 "<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <error>Desktop applications only support the oauth_callback value 'oob'</error>
  <request>/oauth/request_token</request>
</hash>
" UserInfo={NSLocalizedDescription=<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <error>Desktop applications only support the oauth_callback value 'oob'</error>
  <request>/oauth/request_token</request>
</hash>
}

I want show users this: "Unable to authenticate using the system account. User allowed permission to system accounts but there were none set up."

Upvotes: 7

Views: 4589

Answers (4)

Gautam Sareriya
Gautam Sareriya

Reputation: 1833

I am facing the same issue as in the question. I have just set the callBack Url into the Twitter App and resolved the issues.

Go to https://apps.twitter.com/app -> Settings -> Callback URL and Update Settings to save.

Upvotes: 13

Joe Ginley
Joe Ginley

Reputation: 341

You need to use withMethods and specify using the systemAccounts, not webBased or all to use the iOS Twitter settings. The following code is in Swift 3:

twitSharedInstance.logIn(withMethods: .systemAccounts) { (session :TWTRSession?, error :Error?) in
        if (error != nil) {
            if (session != nil) {
                //We have logged into Twitter.
            }
        }
    }

Upvotes: 0

Paweł Tarszewski
Paweł Tarszewski

Reputation: 71

OK, I use this code to notify user about some error:

if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter) {
            if ACAccountStore().accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter).accessGranted {

                Twitter.sharedInstance().logIn{
                    (session, error) in
                    if error != nil {
                        self.showAlert(title: "Twitter - Error", message: (error?.localizedDescription)!)
                        return
                    }

                    guard let token = session?.authToken else { return }
                    guard let secret = session?.authTokenSecret else { return }

                    let credential = FIRTwitterAuthProvider.credential(withToken: token, secret: secret)
                    FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in
                        if error != nil {
                            self.showAlert(title: "Firebase (Twitter)", message: (error?.localizedDescription)!)
                            return
                        }

                        self.showAlert(title: "Firebase (Twitter)", message: "Logged to Firebase via Twitter.")
                    })
                }
            } else {
                showAlert(title: "Twitter - Error", message: "Give access to the system Twitter account.")
            }
        } else {
            showAlert(title: "Twitter - Error", message: "No system accounts set up.")
        }

But it isn't what I want:/

Upvotes: 0

Marie Dm
Marie Dm

Reputation: 2727

I'm not sure I understand what you want to do but you probably want to print the result on the main thread:

Twitter.sharedInstance().logIn{(session, error) in
    DispatchQueue.main.async{
        if error != nil {
            print("Failed to login with Twitter / error:", error!.localizedDescription)
        }else{
            print("succeeded")
        }
    }
}

Upvotes: 0

Related Questions