从Twitter.sharedInstance()获取错误 Swift 3 iOS 10

7

我正在使用Swift 3在iOS 10上编写应用程序。当用户拒绝从系统中授权或未配置帐户(例如,“无法使用系统帐户进行身份验证”)时,sharedInstance()方法会将错误抛出到控制台。在进入闭包之前,错误会显示在控制台上。我不想在应用程序中向用户显示这些错误,例如在警报中。以下是我的代码:

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

我遇到了这个错误:

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>
}

我希望向用户展示以下信息:

"无法使用系统帐户进行身份验证。用户被允许使用系统帐户权限,但没有设置任何系统帐户。"

4个回答

13

我面临与问题相同的困境。我刚刚将回调URL设置到Twitter应用程序中,问题已经解决。

请前往https://apps.twitter.com/app -> 设置 -> 回调URL并更新设置以保存。


2
我需要在回调URL中输入什么? - Alfro
1
任何有效的URL都可以添加。 - Gautam Sareriya

0

我不确定我理解你想做什么,但你可能想在主线程上打印结果:

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

这不是我所想要的。在闭包调用之前错误会显示在控制台上,我对此没有影响力。我不想让用户看到这些错误。控制台上的错误会自动显示出来。 - Paweł Tarszewski
如果您不希望在关闭之前将其打印到控制台,则需要像我的答案中那样添加DispatchQueue.main.async。 - Marie Dm
我仍然无法控制错误,虽然我在控制台上看到了错误信息,但它并不是我的打印函数引起的。 - Paweł Tarszewski
抱歉,我不明白你的意思。 - Marie Dm

0

好的,我使用这段代码来通知用户某些错误:

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

但这不是我想要的:/


0

您需要使用withMethods并指定使用systemAccounts,而不是webBased或all来使用iOS Twitter设置。以下代码是Swift 3:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接