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

4

7

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."

Hourihan answered 29/11, 2016 at 10:49 Comment(0)
E
13

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.

Equity answered 25/5, 2017 at 12:5 Comment(2)
what do I need to input in the call back url?Vaginismus
Any valid URL you can add.Equity
G
0

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")
        }
    }
}
Germanous answered 29/11, 2016 at 10:55 Comment(4)
Is not what i mean. Errors show on console before closures call and i don't have influence on this. I wont show this errors to user. Errors on console shows itself.Dissonancy
If you don't want it to be printed on console before closure, you need to add DispatchQueue.main.async like in my answer.Germanous
Still i don't have control with errors, I got error to console but it isn't my print function.Dissonancy
I'm sorry I don't understand what you mean.Germanous
H
0

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:/

Hourihan answered 29/11, 2016 at 16:22 Comment(0)
A
0

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.
            }
        }
    }
Antihistamine answered 24/3, 2017 at 2:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.